The DataGrid has a property SelectedItems property which can not be used in WPF like 'SelectedItem' because it has no dependency property. I would love to see this be added to WPF.
See as well: https://stackoverflow.com/questions/9880589/bind-to-selecteditems-from-datagrid-or-listbox-in-mvvm
Here the DataGrid is used with SelectionMode "Extended" and using SelectionUnit="FullRow" --> with this configuration I think there should be no issue adding this functionality.
Here a raw template for a simple extension (only supporting to read the 'SelectedItems' property):
``` c#
public class CustomDataGrid : DataGrid
{
public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register(nameof(SelectedItems), typeof(IList), typeof(CustomDataGrid), new PropertyMetadata(default(IList), OnSelectedItemsPropertyChanged));
protected override void OnSelectionChanged(SelectionChangedEventArgs e)
{
base.OnSelectionChanged(e);
SetValue(SelectedItemsProperty, base.SelectedItems);
}
public new IList SelectedItems
{
get => (IList)GetValue(SelectedItemsProperty);
set => throw new Exception("This property is read-only. To bind to it you must use 'Mode=OneWayToSource'.");
}
private static void OnSelectedItemsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((CustomDataGrid)d).OnSelectedItemsChanged((IList)e.OldValue, (IList)e.NewValue);
}
protected virtual void OnSelectedItemsChanged(IList oldSelectedItems, IList newSelectedItems)
{
}
}
```
I was going to open the same issue, glad it is already here.
Just to add, this has inspired MANY workarounds which should be a signal of how much of a problem it is. Examples:
https://tyrrrz.me/blog/wpf-listbox-selecteditems-twoway-binding
http://blog.functionalfun.net/2009/02/how-to-databind-to-selecteditems.html
https://stackoverflow.com/questions/40608981/how-to-get-selected-items-in-wpf-datagrid-using-mvvm
https://stackoverflow.com/questions/28429019/mvvm-datagrid-selecteditems-cant-bind
https://stackoverflow.com/questions/9218657/binding-selecteditems-to-a-observablecollection-property
https://stackoverflow.com/questions/8088595/synchronizing-multi-select-listbox-with-mvvm
Thanks
It would be realy great if you could implement the SelectedItems inside the DataGird/Listbox.
Right now there is, as far as I know, just one solution, that works with virtualisation and a two way binding.
See https://github.com/samueldjack/SelectedItemsBindingDemo
This is the accepted solution from the https://stackoverflow.com/questions/9880589/bind-to-selecteditems-from-datagrid-or-listbox-in-mvvm , that already has been metioned by ice1e0
Most helpful comment
I was going to open the same issue, glad it is already here.
Just to add, this has inspired MANY workarounds which should be a signal of how much of a problem it is. Examples:
https://tyrrrz.me/blog/wpf-listbox-selecteditems-twoway-binding
http://blog.functionalfun.net/2009/02/how-to-databind-to-selecteditems.html
https://stackoverflow.com/questions/40608981/how-to-get-selected-items-in-wpf-datagrid-using-mvvm
https://stackoverflow.com/questions/28429019/mvvm-datagrid-selecteditems-cant-bind
https://stackoverflow.com/questions/9218657/binding-selecteditems-to-a-observablecollection-property
https://stackoverflow.com/questions/8088595/synchronizing-multi-select-listbox-with-mvvm
https://stackoverflow.com/questions/11142976/how-to-support-listbox-selecteditems-binding-with-mvvm-in-a-navigable-applicatio
Thanks