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

前端 未结 9 1225
有刺的猬
有刺的猬 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:50

    I couldn't get Rachel's solution to work how I wanted it, but I found Sandesh's answer of creating a custom dependency property to work perfectly for me. I just had to write similar code for a ListBox:

    public class ListBoxCustom : ListBox
    {
        public ListBoxCustom()
        {
            SelectionChanged += ListBoxCustom_SelectionChanged;
        }
    
        void ListBoxCustom_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SelectedItemsList = SelectedItems;
        }
    
        public IList SelectedItemsList
        {
            get { return (IList)GetValue(SelectedItemsListProperty); }
            set { SetValue(SelectedItemsListProperty, value); }
        }
    
        public static readonly DependencyProperty SelectedItemsListProperty =
           DependencyProperty.Register(nameof(SelectedItemsList), typeof(IList), typeof(ListBoxCustom), new PropertyMetadata(null));
    
    }
    

    In my View Model I just referenced that property to get my selected list.

提交回复
热议问题