WPF Datagrid - 我可以以编程方式选择组中的所有行吗?

时间:2021-03-10 01:38:58

Regards,

I have a DataGrid with a checkbox column to allow the user to select rows, a "select all" checkbox in the column header, and a "select all" checkbox in the group header.

我有一个带有复选框列的DataGrid,允许用户选择行,列标题中的“全选”复选框和组头中的“全选”复选框。

XAML:

 <DataGrid x:Name="TablaDatos"
            AutoGenerateColumns="False" 
            CanUserAddRows="False"
            CanUserReorderColumns="True" 
            CanUserResizeColumns="True" 
            CanUserResizeRows="True" 
            CanUserSortColumns="True"
            SelectionMode="Extended"
            Grid.Row="2">
        <DataGrid.GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Margin" Value="0,0,0,5"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <Expander IsExpanded="True">
                                        <Expander.Background>
                                            <LinearGradientBrush>
                                                <GradientStop Color="#A0FAFAFF" Offset="0.3" />
                                                <GradientStop Color="#FFAACAFF" Offset="1" />
                                            </LinearGradientBrush>
                                        </Expander.Background>
                                        <Expander.Header>
                                            <DockPanel>
                                                <CheckBox x:Name="ProyectoCHK" Margin="0 5 0 0" Click="ProyectoCHK_Click"></CheckBox>
                                                <Label FontWeight="Bold" Content="{Binding Path=Name}" Width="200" Target="{Binding ElementName=ProyectoCHK}"/>
                                                <TextBlock FontWeight="Bold" Text="{Binding Path=ItemCount}" Margin="0 5 0 0"/>
                                            </DockPanel>
                                        </Expander.Header>
                                        <Expander.Content>
                                            <ItemsPresenter />
                                        </Expander.Content>
                                    </Expander>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </DataGrid.GroupStyle>
        <DataGrid.Columns>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.HeaderTemplate>
                    <DataTemplate>
                        <CheckBox/>
                    </DataTemplate>
                </DataGridTemplateColumn.HeaderTemplate>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox IsChecked="{Binding Path=Checked, UpdateSourceTrigger=PropertyChanged}" Name="theCheckbox"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTextColumn IsReadOnly="True" Header="Id. Prueba" Binding="{Binding id_prueba}" SortDirection="Descending"></DataGridTextColumn>
            <DataGridTextColumn IsReadOnly="True" Header="VSAT" Binding="{Binding VSAT}"></DataGridTextColumn>
            <DataGridTextColumn IsReadOnly="True" Header="PC" Binding="{Binding PC}"></DataGridTextColumn>
            <DataGridTextColumn IsReadOnly="True" Header="Fecha Evento" Binding="{Binding FechaUltimoEvento, StringFormat={}\{0:dd/MM/yyyy HH:mm:ss\}}"></DataGridTextColumn>
            <DataGridTextColumn IsReadOnly="True" Header="Estado" Binding="{Binding Estado}"></DataGridTextColumn>
            <DataGridTextColumn IsReadOnly="True" Header="Mensaje" Binding="{Binding Mensaje}"></DataGridTextColumn>
            <DataGridTextColumn IsReadOnly="True" Header="Fecha de creación" Binding="{Binding FechaCreacionPrueba}"></DataGridTextColumn>
            <DataGridTextColumn IsReadOnly="True" Header="Proyecto" Binding="{Binding PROYECTO}"></DataGridTextColumn>
            <DataGridTextColumn IsReadOnly="True" Header="Departamento" Binding="{Binding DEPARTAMENTO}"></DataGridTextColumn>
            <DataGridTextColumn IsReadOnly="True" Header="Municipio" Binding="{Binding MUNICIPIO}"></DataGridTextColumn>
            <DataGridTextColumn IsReadOnly="True" Header="Localidad" Binding="{Binding LOCALIDAD}"></DataGridTextColumn>
            <DataGridTextColumn IsReadOnly="True" Header="Id. punto" Binding="{Binding IDENTIFICADOR_PUNTO}"></DataGridTextColumn>
            <DataGridTextColumn IsReadOnly="True" Header="Usuario" Binding="{Binding UsuarioPrueba}"></DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>

¿How I Can Select all Checkboxes for the rows in a group?

¿我如何选择组中行的所有复选框?

Thanks.

2 个解决方案

#1


0  

In the CheckBox "ProyectoCHK"'s Checked event do this... (code below may need improvements)

在CheckBox“ProyectoCHK”的Checked事件中执行此操作...(以下代码可能需要改进)

  1. The visual sibling of ProyectoCHK is a Label which is bound to the Name property in GroupStyle. Access that and copy its Content into a variable say "GroupValue".

    ProyectoCHK的可视兄弟是一个Label,它绑定到GroupStyle中的Name属性。访问该内容并将其内容复制到变量中,说“GroupValue”。

  2. Assuming that you have bound your DataGrid to a CollectionView, when grouping happens you have the GroupDescription in it.

    假设您已将DataGrid绑定到CollectionView,则在进行分组时,您会在其中包含GroupDescription。

So accessing that will give you the GroupedPropertyName on which grouping has happened. Then you can use

因此访问它将为您提供已发生分组的GroupedPropertyName。然后你可以使用

     var items = DataGrid.ItemsSource.AsQueryable.Where(GroupedPropertyName + " == " + "\"" + GroupValue + "\""); 

AsQueryable() is available in Linq namespace.

Linq命名空间中提供了AsQueryable()。

  1. The items returned by the query filter above, just make individual item.Checked = true (Assuming that item class has implemented INotifyPropertyChanged and has raised PropertyChanged notification for Checked property).
  2. 上面的查询过滤器返回的项目,只是使单个item.Checked = true(假设项类已实现INotifyPropertyChanged并已为Checked属性引发了PropertyChanged通知)。

Let me know if this works.

让我知道这个是否奏效。

#2


0  

If you're using MVVM pattern you can just set the bound CheckBox property in your view-model to "True" and the CheckBox control will reflect the value

如果您正在使用MVVM模式,则只需将视图模型中的绑定CheckBox属性设置为“True”,CheckBox控件将反映该值

#1


0  

In the CheckBox "ProyectoCHK"'s Checked event do this... (code below may need improvements)

在CheckBox“ProyectoCHK”的Checked事件中执行此操作...(以下代码可能需要改进)

  1. The visual sibling of ProyectoCHK is a Label which is bound to the Name property in GroupStyle. Access that and copy its Content into a variable say "GroupValue".

    ProyectoCHK的可视兄弟是一个Label,它绑定到GroupStyle中的Name属性。访问该内容并将其内容复制到变量中,说“GroupValue”。

  2. Assuming that you have bound your DataGrid to a CollectionView, when grouping happens you have the GroupDescription in it.

    假设您已将DataGrid绑定到CollectionView,则在进行分组时,您会在其中包含GroupDescription。

So accessing that will give you the GroupedPropertyName on which grouping has happened. Then you can use

因此访问它将为您提供已发生分组的GroupedPropertyName。然后你可以使用

     var items = DataGrid.ItemsSource.AsQueryable.Where(GroupedPropertyName + " == " + "\"" + GroupValue + "\""); 

AsQueryable() is available in Linq namespace.

Linq命名空间中提供了AsQueryable()。

  1. The items returned by the query filter above, just make individual item.Checked = true (Assuming that item class has implemented INotifyPropertyChanged and has raised PropertyChanged notification for Checked property).
  2. 上面的查询过滤器返回的项目,只是使单个item.Checked = true(假设项类已实现INotifyPropertyChanged并已为Checked属性引发了PropertyChanged通知)。

Let me know if this works.

让我知道这个是否奏效。

#2


0  

If you're using MVVM pattern you can just set the bound CheckBox property in your view-model to "True" and the CheckBox control will reflect the value

如果您正在使用MVVM模式,则只需将视图模型中的绑定CheckBox属性设置为“True”,CheckBox控件将反映该值