无法在WPF DataGrid中选择多行 [英] Unable to select multiple rows in a WPF DataGrid

查看:86
本文介绍了无法在WPF DataGrid中选择多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使我已经设置了 SelectionMode ="Extended" SelectionUnit ="FullRow" ,当我调试 SelectionChanged 事件时,在 SelectedItems 中始终只有一个选定的项目.

Even though I've got SelectionMode="Extended" and SelectionUnit="FullRow" set, when I debug the SelectionChanged event, there's always only one selected item in SelectedItems.

这是我的 DataGrid :

<DataGrid Grid.Row="0" AutoGenerateColumns="False" Margin="5,5,5,0"
        Name="dgrMembersClub1" ItemsSource="{Binding .}" CanUserAddRows="False"
        SelectionMode="Extended" SelectionUnit="FullRow" SelectionChanged="Grid_SelectionChanged">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Joining" >
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox IsChecked="{Binding IsSelected}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTextColumn IsReadOnly="True" Header="Surname" Binding="{Binding Surname}" />
        <DataGridTextColumn IsReadOnly="True" Header="Name" Binding="{Binding Name}" />
        <DataGridTextColumn IsReadOnly="True" Header="Club" Binding="{Binding Club_Id, Converter={StaticResource ClubName}}" />
        <DataGridTextColumn IsReadOnly="True" Header="City" Binding="{Binding City}" />
    </DataGrid.Columns>
</DataGrid>

和我的 Grid_SelectionChanged 事件:

private void Grid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    DataGrid grid = (DataGrid)sender;
    var test = grid.SelectedItems; //Count == 1 (always)
}

我确实有触发器设置(在App.xaml中),可以更改选定行和交替行的背景和前景画笔.如果相关,请告诉我,我将添加代码.

I do have Triggers set (in App.xaml) that change the background and foreground brushes for selected and alternating rows. If that's relevant, please let me know and I'll add the code.

*编辑*

当您在使用它时,我可以使用一些帮助来使单元格模板中的复选框也起作用.很好,请:)

While you're at it, I could use some help getting the checkbox in the cell template to work too. Pretty please :)

推荐答案

DataGrid的SelectedItems属性包含一个列表,列出了选中的项目...

The SelectedItems property of the DataGrid contains a list of, well, selected items...

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 dog = item as Dog;
    }
}

此指示性事件处理程序获取SelectedItems并遍历它.

This indicative event handler gets the SelectedItems and loops through it.

但是,有一个警告:

如果SelectionMode属性设置为Single,则SelectedItems列表将仅包含SelectedItem属性值."

"If the SelectionMode property is set to Single, the SelectedItems list will contain only the SelectedItem property value."

来源: SelectedItems属性继承自IList,因此可以对其进行强制转换并对其执行LINQ操作.它也适用于非连续选择.

The SelectedItems property inherits from IList so it is possible to cast it and perform LINQ operations on it as well. It also works fine with non-contiguous selections.

http://www.dotnetperls.com/datagrid

这篇关于无法在WPF DataGrid中选择多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆