Gu.Wpf.DataGrid2D icon indicating copy to clipboard operation
Gu.Wpf.DataGrid2D copied to clipboard

Get list of all selected cells

Open Pyth0ff opened this issue 5 years ago • 4 comments

It is currenly only possible via Selected.CellItem to get an single cell item. I want to bind multiple selected cells to my ViewModel, which would require something like Selected.CellItems.

Pyth0ff avatar Oct 09 '19 13:10 Pyth0ff

Sounds useful even if not common. What type should be used for CellItems maybe ReadOnlyObservableCollection<object> is best.

JohanLarsson avatar Oct 09 '19 14:10 JohanLarsson

Yes, ReadOnlyObservableCollection<object> sounds good.

Pyth0ff avatar Oct 09 '19 14:10 Pyth0ff

I'm not gonna have time anytime soon, do you feel like trying your hand at a PR?

JohanLarsson avatar Oct 09 '19 15:10 JohanLarsson

A different approach to multi-selection - which is facilitated for example by ListView, might be to have an IsSelected property on the ViewModel, which can be set if the cell is selected. In the VM, you can then easily retrieve a complete list of selected items. I think this approach can also be applied with DataGrid: https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.datagridcell.isselected?view=netframework-4.8 For a ListView, it then looks like this:

<ListView.Resources>
  <Style TargetType="ListViewItem">
    <Setter Property="IsSelected" Value="{Binding Selected, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" />
  </Style>
</ListView.Resources>

You can thus simply add a 'Selected' property or so to your VM. This should be applicable to DataGrid also. Of course, this presupposes you bind a ViewModel to the cells of the 2D GridView.

mennowo avatar Oct 19 '19 19:10 mennowo