How to support ListBox SelectedItems binding with MVVM in a navigable application

前端 未结 9 1221
有刺的猬
有刺的猬 2020-11-27 15:06

I am making a WPF application that is navigable via custom \"Next\" and \"Back\" buttons and commands (i.e. not using a NavigationWindow). On one screen, I have

9条回答
  •  死守一世寂寞
    2020-11-27 15:47

    Here's yet another solution. It's similar to Ben's answer, but the binding works two ways. The trick is to update the ListBox's selected items when the bound data items change.

    public class MultipleSelectionListBox : ListBox
    {
        public static readonly DependencyProperty BindableSelectedItemsProperty =
            DependencyProperty.Register("BindableSelectedItems",
                typeof(IEnumerable), typeof(MultipleSelectionListBox),
                new FrameworkPropertyMetadata(default(IEnumerable),
                    FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnBindableSelectedItemsChanged));
    
        public IEnumerable BindableSelectedItems
        {
            get => (IEnumerable)GetValue(BindableSelectedItemsProperty);
            set => SetValue(BindableSelectedItemsProperty, value);
        }
    
        protected override void OnSelectionChanged(SelectionChangedEventArgs e)
        {
            base.OnSelectionChanged(e);
            BindableSelectedItems = SelectedItems.Cast();
        }
    
        private static void OnBindableSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is MultipleSelectionListBox listBox)
                listBox.SetSelectedItems(listBox.BindableSelectedItems);
        }
    }
    

    Unfortunately, I wasn't able to use IList as the BindableSelectedItems type. Doing so sent null to my view model's property, whose type is IEnumerable.

    Here's the XAML:

    
    

    There's one thing to watch out for. In my case, a ListBox may be removed from the view. For some reason, this causes the SelectedItems property to change to an empty list. This, in turn, causes the view model's property to be changed to an empty list. Depending on your use case, this may not be desirable.

提交回复
热议问题