使用Caliburn.Micro在DataGrid中选择多个记录

时间:2021-06-13 15:43:55

I have a WPF application using Caliburn.Micro.

我有一个使用Caliburn.Micro的WPF应用程序。

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

DataGrid有一个属性SelectedItem =“{Binding Path = SelectedUsageRecord}”

As you can see, SelectedItem is bound to SelectedUsageRecord property. But I need to be able to handle selecting multiple records. Is this possible to bind multiple records to a collection property? I don't see anything like "SelectedItems"... Thanks.

如您所见,SelectedItem绑定到SelectedUsageRecord属性。但我需要能够处理选择多个记录。这可以将多个记录绑定到集合属性吗?我没有看到像“SelectedItems”这样的东西......谢谢。

2 个解决方案

#1


6  

Here's what I did after striking the same scenario as you. In short handle the selection change event directly and pull the selected rows from the event args. Assuming a source collection of "Rows" each one being a RowViewModel, and a collection for the "_selectedRows".

这是我和你一样的情景后所做的。简而言之,直接处理选择更改事件并从事件args中提取所选行。假设“Rows”的源集合,每个都是RowViewModel,以及“_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);
    }
}

#2


3  

I just wanted to post my solution. In Caliburn micro there is no need to set the source as long as you stay true to the naming convention.

我只是想发布我的解决方案。在Caliburn micro中,只要您坚持命名约定,就不需要设置源代码。

Xaml

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

C#

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));
}

#1


6  

Here's what I did after striking the same scenario as you. In short handle the selection change event directly and pull the selected rows from the event args. Assuming a source collection of "Rows" each one being a RowViewModel, and a collection for the "_selectedRows".

这是我和你一样的情景后所做的。简而言之,直接处理选择更改事件并从事件args中提取所选行。假设“Rows”的源集合,每个都是RowViewModel,以及“_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);
    }
}

#2


3  

I just wanted to post my solution. In Caliburn micro there is no need to set the source as long as you stay true to the naming convention.

我只是想发布我的解决方案。在Caliburn micro中,只要您坚持命名约定,就不需要设置源代码。

Xaml

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

C#

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));
}