1

Is there any way of getting the items of a multiselect in a listbox without using the code behind, but whith just the binding?

I know that I can do a foreach on the code behind etc.. But I'm guessing if there is a cleaner solution with just the binding between XAML and ViewModel.

On some of the listboxes the IsSelected property is used to set multiple items selected at the page loading.

Thanks for the help.

  • 1
    [How to support ListBox SelectedItems binding with MVVM in a navigable application](https://stackoverflow.com/questions/11142976/how-to-support-listbox-selecteditems-binding-with-mvvm-in-a-navigable-applicatio) – Lei Yang Mar 12 '19 at 14:16
  • You could [implement a behaviour to bind to read-only properties](https://blog.magnusmontin.net/2014/01/30/wpf-using-behaviours-to-bind-to-readonly-properties-in-mvvm/) – mm8 Mar 12 '19 at 14:35

1 Answers1

1

You can send the SelectedItems as a command parameter. For example, you can get the listbox's SelectedItems in a button's command like this.

<ListBox x:Name="listbox" ItemsSource="{Binding MyList}" SelectionMode="Multiple"/>
<Button x:Name="btn" Command="{Binding MyCommand}" CommandParameter="{Binding SelectedItems, ElementName=listbox}" Content="Get Selected Items"/>
Mathivanan KP
  • 1,979
  • 16
  • 25