无法在 WPF DataGrid 中选择多行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24334451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:58:05  来源:igfitidea点击:

Unable to select multiple rows in a WPF DataGrid

c#wpfdatagridselecteditem

提问by DerpyNerd

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

即使我已经得到SelectionMode="Extended"SelectionUnit="FullRow"设置,当我调试SelectionChanged事件时,在SelectedItems.

This is my DataGrid:

这是我的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>

And my Grid_SelectionChangedevent:

还有我的Grid_SelectionChanged活动:

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

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.

我确实设置了触发器(在 App.xaml 中),用于更改选定行和交替行的背景和前景画笔。如果这是相关的,请告诉我,我会添加代码。

* EDIT *

* 编辑 *

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

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

采纳答案by Gayot Fow

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

DataGrid 的 SelectedItems 属性包含一个包含所选项目的列表...

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

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

这个指示性事件处理程序获取 SelectedItems 并循环遍历它。

However, there's a caveat:

但是,有一个警告:

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

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

Source: http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid.selecteditems(v=vs.95).aspx

来源:http: //msdn.microsoft.com/en-us/library/system.windows.controls.datagrid.selecteditems(v=vs.95).aspx

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.

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

More tips at http://www.dotnetperls.com/datagrid

更多提示请访问http://www.dotnetperls.com/datagrid

回答by ehsan A

private IEnumerable<DataGridRow> GetDataGridRows(DataGrid grid)
{
    var itemsSource = grid.ItemsSource as IEnumerable;
    if (null == itemsSource) yield return null;
    foreach (var item in itemsSource)
    {
        var row = grid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow;
        if (null != row & row.IsSelected) yield return row;
    }
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    var rows = GetDataGridRows(dgv_Students);
    string id; //Sample =>"85-999888-2"
    foreach (DataGridRow dr in rows)
    {
        id = (dr.Item as tbl_student).code_meli;
        MessageBox.Show(id);
    }
}