Open aguahombre opened 5 years ago
SelectedItems
property isn't bindable in WPF in DataGrid.
@FoggyFinder is correct, SelectedItems is not bindable.
I was looking at the WPF MultiSelector source code and SelectedItems is marked with Bindable attribute; I should have checked it in practice.
I still think it should be bindable in Avalonia.
With DevExpress GridControl it is. It should be on Avalonia DataGrid too!
My team is currently migrating from WPF to Avalonia. Not being able to bind to the SelectedItems property is a pain for us. Is this planned to be changes in an upcoming version?
There are no plans to change it. PRs are welcomed though.
@Boschindi as far as I remember the situation wasn't easier in WPF. You had to write a behavior or attached property for it as well. For sure there are 3rd party libs in WPF that support it.
One more hint: Check also Avalonia.TreeDataGrid, it has much better performance but a quite different API. Could be worth to take a look anyway.
@timunie you're right. This behavior is rather part of our 3rd party UI component lib. '-_-
We changed our view models to follow the WPF way, to store the selection state inside of the nodes. During our migration to Avalonia UI this hopefully also works properly in the DataGrid.
The Avalonia.TreeDataGrid does not come with the desired binding support for our MVVM approach. We'll stick to the DataGrid.
Thanks for your support.
Workaround I adapted for Avalonia.
View:
...
<Button Command="{Binding MyCommand}"
CommandParameter="{Binding ElementName=MyGrid, Path=SelectedItems}" />
<DataGrid Name="MyGrid"
SelectionMode="Extended" />
...
ViewModel:
...
[RelayCommand]
private void My(IList selectedItems)
{
foreach (MyModel i in selectedItems)
{
...
}
}
...
The actual collection passed to the method is of type DataGridSelectedItemsCollection which appears to be internal. So we take non-generic IList as an argument and cast each item to our model.
The DataGrid SelectedItems property is neither bindable of observable in Avalonia. In WPF it is both, so can be used to update the UI; e.g. Command handlers.