Unable to select multiple rows in a WPF DataGrid

核能气质少年 提交于 2021-02-11 06:42:11

问题


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.

This is my 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:

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.

* EDIT *

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


回答1:


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

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

However, there's a caveat:

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

Source: 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.

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




回答2:


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


来源:https://stackoverflow.com/questions/24334451/unable-to-select-multiple-rows-in-a-wpf-datagrid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!