当SelectedItem
值改变时,它不会触发SelectedItemConverter
。
我试着使用SetCurrentValue
和InvalidateProperty
,并摆弄了NotifyOnSourceUpdated
、UpdateSourceTrigger
和Mode
等属性。
可悲的是,他们不工作。
也许我应该在parent xaml中添加一些东西来推动它?
<local:Menu
SelectedItem="{Binding SelectedMenuItem}"
OnSelect="{Binding OnSelectCommand}" Items="{Binding MenuItems}"/>
谢谢你的帮助。
控制xaml
<UserControl>
<UserControl.Resources>
<vm:MockMenuViewModel x:Key="DesignViewModel"/>
<cv:SelectedItemConverter x:Key="SelectedItemConverter"/>
</UserControl.Resources>
<ItemsControl
d:DataContext="{Binding Source={StaticResource DesignViewModel}}"
ItemsSource="{Binding Path=MenuItems, Mode=OneWay}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel></StackPanel>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type MenuItem}">
<Button Content="{Binding Name}"
Command="{Binding OnSelect, RelativeSource={RelativeSource AncestorType=UserControl}}"
CommandParameter="{Binding}">
<Button.IsEnabled>
<MultiBinding Converter="{StaticResource SelectedItemConverter}"
NotifyOnSourceUpdated="True"
NotifyOnTargetUpdated="True" Mode="OneWay">
<Binding Path="SelectedItem" UpdateSourceTrigger="PropertyChanged" Mode="OneWay"/>
<Binding Path="." Mode="OneWay"/>
</MultiBinding>
</Button.IsEnabled>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</UserControl>
控件cs
public partial class Menu : UserControl, INotifyPropertyChanged
{
public event PropertyChangedEventHandler? PropertyChanged;
public Menu()
{
InitializeComponent();
}
public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register(
"SelectedItem",
typeof(MenuItem),
typeof(Menu),
new PropertyMetadata(null, OnSelectedItemChanged));
public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register(
"Items",
typeof(IEnumerable<MenuItem>),
typeof(Menu),
new FrameworkPropertyMetadata(null));
public static readonly DependencyProperty OnSelectProperty = DependencyProperty.Register(
"OnSelect",
typeof(ICommand),
typeof(Menu),
new PropertyMetadata(null, OnSelectChanged));
public MenuItem SelectedItem
{
get => (MenuItem)GetValue(SelectedItemProperty);
set
{
SetValue(SelectedItemProperty, value);
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Items)));
}
}
public IEnumerable<MenuItem> Items
{
get => (IEnumerable<MenuItem>)GetValue(ItemsProperty);
set => SetValue(ItemsProperty, value);
}
public ICommand OnSelect
{
get => (ICommand)GetValue(OnSelectProperty);
set => SetValue(OnSelectProperty, value);
}
public static void OnSelectedItemChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs eventArgs)
{
var menu = dependencyObject as Menu;
if (menu == null)
{
return;
}
menu.InvalidateProperty(SelectedItemProperty);
menu.InvalidateProperty(ItemsProperty);
}
public static void OnSelectChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs eventArgs)
{
var menu = dependencyObject as Menu;
if (menu == null)
{
return;
}
menu.SetCurrentValue(OnSelectProperty, eventArgs.NewValue);
}
}
1条答案
按热度按时间2jcobegt1#
如果您尝试系结至父项
Menu
UserControl
的SelectedItem
属性,您应该指定RelativeSource
: