简体   繁体   English

为什么我的依赖属性绑定没有按预期工作?

[英]Why is my Dependency Property Binding is not working as expected?

I am trying to make a customized UserControl which includes a ListView.我正在尝试制作一个包含 ListView 的自定义 UserControl。 I would like to bind the SelectedItem Property to my View Model.我想将 SelectedItem 属性绑定到我的视图 Model。 I therefore created a DP in my User Control and bound the SelectedItem Property of the ListView to the new Dependency Property.因此,我在我的用户控件中创建了一个 DP,并将 ListView 的 SelectedItem 属性绑定到新的 Dependency 属性。

        public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register(
            "SelectedItem",
            typeof(object), 
            typeof(DragAndDropListView),
            new PropertyMetadata(SelectedItemPropertyChangedCallback));

        private static void SelectedItemPropertyChangedCallback(DependencyObject d, 
        DependencyPropertyChangedEventArgs e)
        {
            if (d is DragAndDropListView dragAndDropListView)
            {
            }
        }

        public object SelectedItem
        {
            get => GetValue(SelectedItemProperty);
            set => SetValue(SelectedItemProperty, value);
        }

In the xaml code I then made a binding然后在 xaml 代码中,我进行了绑定

            <ListView Name="ListView"
                      PreviewMouseLeftButtonDown="ListViewPreviewMouseLeftButtonDown"
                      AllowDrop="True"
                      MouseMove="ListViewMouseMove"
                      DragEnter="ListViewDragEnter"
                      Drop="ListViewDrop"
                      SelectedItem="{Binding Path= SelectedItem}"/>

And to make sure it works I assigned the UserControl as DataContext to the ListView为了确保它有效,我将 UserControl 作为 DataContext 分配给 ListView

        public DragAndDropListView()
        {
            InitializeComponent();
            ListView.ItemsSource = Items;
            ListView.DataContext = this;
        }

And then I added a binding where I use the UserControl.然后我在使用 UserControl 的地方添加了一个绑定。

       <userControls:DragAndDropListView 
                Items="{Binding SelectedCustomers}" 
                SelectedItem="{Binding SelectedCustomer}"
                DisplayMemberPath="Name"
                Grid.Column="0"
                Grid.Row="0"/>

As you may see I thought, that I don't need a SelectedItemPropertyChangedCallback.如您所见,我认为我不需要 SelectedItemPropertyChangedCallback。 First I didn't even implement it.首先,我什至没有实施它。 I added it later to add a break point to see if it even works.我后来添加了它以添加一个断点以查看它是否有效。 When I select an item in the listView it calls the callback.当我 select 列表视图中的一个项目时,它会调用回调。 But the setter of the SelectedCustomer in my ViewModel is never called.但是我的 ViewModel 中的 SelectedCustomer 的设置器永远不会被调用。

I would expect this to work.我希望这能奏效。 Please help me to understand my mistake.请帮助我理解我的错误。

The Binding must be TwoWay:绑定必须是双向的:

SelectedItem="{Binding SelectedCustomer, Mode=TwoWay}"

You could declare the property such that it binds TwoWay by default:您可以声明该属性,使其默认绑定 TwoWay:

public static readonly DependencyProperty SelectedItemProperty =
    DependencyProperty.Register(
        nameof(SelectedItem),
        typeof(object), 
        typeof(DragAndDropListView),
        new FrameworkPropertyMetadata(
            null,
            FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
            SelectedItemPropertyChangedCallback));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM