简体   繁体   English

无法在 WPF DataGrid 中选择多行

[英]Unable to select multiple rows in a WPF DataGrid

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 .即使我设置了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_SelectionChanged event:我的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.我确实设置了触发器(在 App.xaml 中),用于更改选定行和交替行的背景和前景画笔。 If that's relevant, please let me know and I'll add the code.如果这是相关的,请告诉我,我会添加代码。

* EDIT * * 编辑 *

While you're at it, I could use some help getting the checkbox in the cell template to work too.当您使用它时,我可以使用一些帮助来使单元格模板中的复选框也起作用。 Pretty please :)漂亮请:)

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. SelectedItems 属性继承自 IList,因此可以对其进行强制转换并对其执行 LINQ 操作。 It also works fine with non-contiguous selections.它也适用于非连续选择。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM