AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
26.05k stars 2.25k forks source link

Make DataGrid SelectedItems bindable and observable #2890

Open aguahombre opened 5 years ago

aguahombre commented 5 years ago

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.

FoggyFinder commented 5 years ago

SelectedItems property isn't bindable in WPF in DataGrid.

aguahombre commented 5 years ago

@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.

EtherGhost commented 2 years ago

With DevExpress GridControl it is. It should be on Avalonia DataGrid too!

Boschindi commented 1 year ago

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?

maxkatz6 commented 1 year ago

There are no plans to change it. PRs are welcomed though.

timunie commented 1 year ago

@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.

Boschindi commented 1 year ago

@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.

rowlul commented 2 months ago

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.