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

前端 未结 9 1217
有刺的猬
有刺的猬 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 16:03

    I kept looking into an easy solution for this but with no luck.

    The solution Rachel has is good if you already have the Selected property on the object within your ItemsSource. If you do not, you have to create a Model for that business model.

    I went a different route. A quick one, but not perfect.

    On your ListBox create an event for SelectionChanged.

    
    

    Now implement the event on the code behind of your XAML page.

    private void lstBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var listSelectedItems = ((ListBox) sender).SelectedItems;
        ViewModel.YourListThatNeedsBinding = listSelectedItems.Cast().ToList();
    }
    

    Tada. Done.

    This was done with the help of converting SelectedItemCollection to a List.

提交回复
热议问题