1

I am trying to make a customized UserControl which includes a ListView. I would like to bind the SelectedItem Property to my View Model. I therefore created a DP in my User Control and bound the SelectedItem Property of the ListView to the new Dependency Property.

        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

            <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

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

And then I added a binding where I use the 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. 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. But the setter of the SelectedCustomer in my ViewModel is never called.

I would expect this to work. Please help me to understand my mistake.

0

1 Answer 1

5

The Binding must be TwoWay:

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

You could declare the property so that it binds TwoWay by default:

public static readonly DependencyProperty SelectedItemProperty =
    DependencyProperty.Register(
        nameof(SelectedItem),
        typeof(object), 
        typeof(DragAndDropListView),
        new FrameworkPropertyMetadata(
            null,
            FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
            SelectedItemPropertyChangedCallback));
Sign up to request clarification or add additional context in comments.

1 Comment

Oh my I did not think about that at all. Thank you very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.