简体   繁体   中英

WPF Datagrid - Can I programmatically select all rows in a Group?

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.

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.

如果您使用的是MVVM模式,则可以将视图模型中的绑定CheckBox属性设置为“ True”,并且CheckBox控件将反映该值

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

  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".

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

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

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

AsQueryable() is available in Linq namespace.

  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).

Let me know if this works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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