wpf ItemsControl中未调用多绑定转换器

kh212irz  于 2022-12-05  发布在  其他
关注(0)|答案(1)|浏览(165)

SelectedItem值改变时,它不会触发SelectedItemConverter
我试着使用SetCurrentValueInvalidateProperty,并摆弄了NotifyOnSourceUpdatedUpdateSourceTriggerMode等属性。
可悲的是,他们不工作。
也许我应该在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);
        }
    }
2jcobegt

2jcobegt1#

如果您尝试系结至父项MenuUserControlSelectedItem属性,您应该指定RelativeSource

<Binding Path="SelectedItem"
    RelativeSource="{RelativeSource AncestorType=UserControl}"
    UpdateSourceTrigger="PropertyChanged" Mode="OneWay"/>

相关问题