How to bind SelectedItems in ListBox to ViewModel [duplicate]

3 weeks ago 30
ARTICLE AD BOX

I have a ListBox

<ListBox BorderBrush="Transparent" FontSize="{Binding ExplorerElementsHeight}" ItemsSource="{Binding CurrentExplorerContent}" SelectedItems="{Binding SelectedFileEntities, Mode=OneWayToSource}" SelectionMode="Multiple" Style="{StaticResource SzFilesExplorerListBox}"> <!-- Explorer list box behaviors --> <b:Interaction.Behaviors> <behaviors:ItemDoubleClickBehavior Command="{Binding GetContentCommand}" /> <behaviors:ControlPlusMouseWheelBehavior Command="{Binding ChangeExplorerElementsHeightCommand}" /> </b:Interaction.Behaviors> </ListBox>

And the ViewModel's property:

[ObservableProperty] private ObservableCollection<FileEntity> _selectedFileEntities = [];

And I need to bind (at least one way) SelectedItems to ViewModel, but I can't because SelectedItems has no setter, I can't use "code-behind" due to ViewModelLocator.
I tried to create a behavior but SelectedItems is non-generic IList and I can put in to SelectedFileEntities.

Read Entire Article