WPF - 如何从 C# 中的 DataGrid 中的多个选定行获取单元格内容 - xaml

WPF - How to get cell contents from multiple selected rows in a DataGrid in C# - xaml

我希望能够 select 此 DataGrid 中的多行,然后检索出现在 select 单元格中的数据。我在网上找到的所有内容都不起作用。我尝试使用 'SelectionChanged' 和 'SelectedCellsChanged' 之类的事件参数来获取行索引,但它的行为方式不允许我跟踪所有 selected.

有没有人能建议一种方法?提前致谢。

数据网格代码:

<DataGrid x:Name="walkGrd" ItemsSource="{Binding overviewGrd}" RowHeaderWidth="0" AutoGenerateColumns="True" ColumnWidth="*" CanUserAddRows="False" CanUserDeleteRows="False" CanUserReorderColumns="False" CanUserSortColumns="False"  IsReadOnly="True" SelectionMode="Extended" SelectionUnit="FullRow" Margin="1,63,0,37"  RowHeight="18" FontSize="13" BorderBrush="Black" AreRowDetailsFrozen="True">
        <DataGrid.Resources>
            <Style TargetType="{x:Type DataGridCell}">
                <Setter Property="BorderThickness" Value="0"/>
                <Setter Property="Margin" Value="0,0,-1,0"/>
                <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Background" Value="DarkGray"/>
                        <Setter Property="Foreground" Value="White"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
            <Style x:Key="Body_Content_DataGrid_Centering"
                TargetType="{x:Type DataGridCell}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type DataGridCell}">
                            <Grid Background="{TemplateBinding Background}">
                                <ContentPresenter VerticalAlignment="Center" />
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </DataGrid.Resources>
        <DataGrid.Columns>
            <DataGridTextColumn Header="Identifier" TextBlock.TextAlignment="Justify" Binding="{Binding one}" Width="0.25*" />
            <DataGridTextColumn Header="ID OID" Binding="{Binding two}" Width="0.25*" />
            <DataGridTextColumn Header="Up Time" Binding="{Binding three}" Width="0.25*" />
            <DataGridTextColumn Header="Time OID" Binding="{Binding four}" Width="0.25*" />

        </DataGrid.Columns>
    </DataGrid>
private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    //Get SelectedItems from DataGrid.
    var grid = sender as DataGrid;
    var selected = grid.SelectedItems;

    foreach (var item in selected)
    {
        var item = item as ClassName;
    }
}

SelectionMode 设置为 Multiple

我在这个 post 中找到了解决方案 - DataGrid get selected rows' column values

如果取消选择一行,它也会处理。

private void walkGrd_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
    foreach (var item in e.AddedCells)
    {
         var col = item.Column as DataGridColumn;
         var fc = col.GetCellContent(item.Item);

         if (fc is TextBlock)
         {
             Console.WriteLine("Values" + (fc as TextBlock).Text);
         }
         //// Like this for all available types of cells
    }
    foreach (var item in e.RemovedCells)
    {
         var col = item.Column as DataGridColumn;
         var fc = col.GetCellContent(item.Item);

         if (fc is TextBlock)
         {
            Console.WriteLine("Values Removed" + (fc as TextBlock).Text);
         }
         //// Like this for all available types of cells
    }   
}