I have a WPF data grid with multi select (SelectedMode = Extended). Each item has an IsSelected binding per https://stackoverflow.com/a/2615487/284795
<DataGrid
ItemsSource="{Binding Items}"
SelectionUnit="FullRow"
SelectionMode="Extended"
SelectedIndex="{Binding SelectedIndex}"
SelectedItem="{Binding SelectedItem}">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="IsSelected" Value="{Binding IsSelected}" />
</Style>
</DataGrid.RowStyle>
</DataGrid>
I’m curious. How do the properties SelectedIndex and SelectedItem on the DataGrid now behave? If two items are selected in the data grid, which one does SelectedItem point to?
Also, if all these bindings are two way, and I make a change to one from a view model, will the others be updated? (I’m observing a bug in my app perhaps because of this)
I had the same question a while ago, and I checked: I created a
DataGridwith several items and set theSelectedMode = Extended, and I mde a binding to both:SelectedItemandSelectedIndexproperties.The result was this: When you select a single item, and then you select other items and make a multiselect, the
SelectedItemandSelectedIndexproperties will be the first item that you selected. So when you make multiselects the selected item will be the first one you selected.Also all other selected items will be in the
SelectedItemscollection, that is read only (like in the question you pointed said) and it is not a dependency property, so you can’t make bindings to it. So if you want take all selected items, you need to handle the selection changed event of theDataGridand then, manually, add and remove the new and old items from the collection that you want to keep (selected items).Hope my answer be clear enough, and it could helps you…