Selecting multiple records in a DataGrid using Caliburn.Micro

I have a WPF application using Caliburn.Micro.

The DataGrid has an attribute SelectedItem = "{Binding Path = SelectedUsageRecord}"

As you can see, SelectedItem is bound to the SelectedUsageRecord property. But I need to be able to handle the selection of multiple records. Is it possible to associate multiple records with collection property? I don't see anything like "SelectedItems" ... Thanks.

+4
source share
2 answers

This is what I did after I made the same script as you. In the short handle, the selection event will change directly and pull the selected rows from the event arguments. Suppose that the original set of "Strings" each of them is a RowViewModel and a collection for "_selectedRows".

<DataGrid RowsSource="{Binding Rows}" x:Name="Rows" SelectionMode="Extended" SelectionUnit="FullRow"> <i:Interaction.Triggers> <i:EventTrigger EventName="SelectionChanged"> <cal:ActionMessage MethodName="SelectedRowsChangeEvent"> <cal:Parameter Value="$eventArgs" /> </cal:ActionMessage> </i:EventTrigger> </i:Interaction.Triggers> </DataGrid> 
 public void SelectedRowsChangeEvent(SelectionChangedEventArgs e) { foreach (var addedRow in e.AddedRows) { _selectedRows.Add(addedRow as RowViewModel); } foreach (var removedRow in e.RemovedRows) { _selectedRows.Remove(removedRow as RowViewModel); } } 
+6
source

I just wanted to post my decision. Caliburn micro does not need to set the source until you return to the naming convention.

Xaml

 <DataGrid x:Name="Rows" SelectionMode="Extended" cal:Message.Attach="[Event SelectionChanged] = [Row_SelectionChanged($eventArgs)]"> 

FROM#

 public List<MyObject> Rows { get; set; } public MyObject SelectedRow { get; set; } //Will be set by Caliburn Micro. No need to use "SelectedItem={...}" List<MyObject> _selectedObjects = new List<MyObject>(); public void Row_SelectionChanged(SelectionChangedEventArgs obj) { _selectedObjects.AddRange(obj.AddedItems.Cast<MyObject>()); obj.RemovedItems.Cast<MyObject>().ToList().ForEach(w => _selectedObjects.Remove(w)); } 
+2
source

Source: https://habr.com/ru/post/1413956/


All Articles