I want to get some selected rows items & try to manipulate them. Currently SelectedItem is giving me only one row at a time. And SelectedItems is not a dependency property. I found a solution by creating our own dependency property to get selected items. Is there any option apart from this?
Asked
Active
Viewed 5,669 times
2
-
2I think answer for your question is here http://stackoverflow.com/questions/22868445/wpf-binding-selecteditems-in-mvvm – DT sawant Nov 15 '16 at 12:09
-
SelectedItems is a **readonly** dependency property, which is the problem. – StayOnTarget Jul 20 '20 at 16:55
-
Does this answer your question? [Bind to SelectedItems from DataGrid or ListBox in MVVM](https://stackoverflow.com/questions/9880589/bind-to-selecteditems-from-datagrid-or-listbox-in-mvvm) – StayOnTarget Jul 20 '20 at 16:58
1 Answers
10
Another possible solution is to add an IsSelected property onto the items your showing in your grid
public bool IsSelected
{
get { return _isSelected; }
set
{
RaisePropertyChanged(_isSelected, value);
}
}
and to then add a style onto the data grid row to change that property.
<Style TargetType="{x:Type DataGridRow}" >
<Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}" />
</Style>
Then to get the currently selected items:
var selectedItems = Items.Where(i => i.IsSelected).ToList();

emybob
- 1,303
- 15
- 22