There is a model in which there is a list of files. This list is for ListBox . How to get a list of selected rows if SelectionMode="Extended" ?

  • SelectedItems="{Binding Selecteditems}" ? - VladD
  • so there is no SelectedItems for ListBox , there is only SelectedItem ... - MaximK
  • one
    but WPF does not see it / Binding to it I can’t do it, but it can be added - MaximK

1 answer 1

I was not the first to encounter this problem.

Here is the solution through CustomListBox: https://stackoverflow.com/questions/22868445/wpf-binding-selecteditems-in-mvvm


Create an inheritor from ListBox and add DependencyProperty

 public class CustomListBox: ListBox { public CustomListBox() { this.SelectionChanged += CustomListBox_SelectionChanged; } private void CustomListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { this.SelectedItemsList = this.SelectedItems; } public IList SelectedItemsList { get { return (IList)GetValue(SelectedItemsListProperty); } set { SetValue(SelectedItemsListProperty, value); } } // Using a DependencyProperty as the backing store for SelectedItemsList. This enables animation, styling, binding, etc... public static readonly DependencyProperty SelectedItemsListProperty = DependencyProperty.Register("SelectedItemsList", typeof(IList), typeof(CustomListBox), new PropertyMetadata(null)); } 

XAML:

 <Windows ... xmlns:customObjects ="clr-namespace:Preparation.CustomObjects"/> <customObjects:CustomListBox x:Name="listBox" SelectionMode="Extended" ItemsSource="{Binding Planshets}" SelectedItemsList="{Binding SelectedPlanshets, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> 

ViewModel:

 private IList _selectedPlanshets = new ArrayList(); public IList SelectedPlanshets { get { return _selectedPlanshets; } set { _selectedPlanshets = value; RaisePropertyChanged("SelectedPlanshets"); } } 

Now in our model the ViewModel.SelectedPlanshet property is available.