samedi 4 avril 2020

How to Select all rows in datagrid with checkbox (wpf)?

im trying to select all rows in datagrid with checkbox using this code, when i check the checkbox all rows are selected but without the tick marK.

This is my xaml column

 <DataGridTemplateColumn>
                            <DataGridTemplateColumn.Header>
                                <CheckBox Checked="chkSelectAll_Checked" Unchecked="chkSelectAll_Unchecked" Style="{StaticResource NVStyleCheckBoxAssistant}"></CheckBox>
                            </DataGridTemplateColumn.Header>
                            <DataGridTemplateColumn.CellTemplate >
                                <DataTemplate>
                                    <CheckBox IsChecked="{Binding Association_Click}" Style="{StaticResource NVStyleCheckBoxAssistant}"/>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
 </DataGridTemplateColumn>

Code behind

 Private Sub chkSelectAll_Checked(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Association_Click(sender, e)
        dgv1.SelectAll()
 End Sub

This is the style

 <Style x:Key="NVStyleCheckBoxAssistant"  TargetType="{x:Type CheckBox}">
    <Setter Property="VerticalAlignment" Value="center"/>
    <Setter Property="HorizontalAlignment" Value="center"/>
    <Setter Property="IsChecked"  Value="{Binding EstSelectionner, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type CheckBox}">
                <BulletDecorator Background="Transparent"    SnapsToDevicePixels="true">
                    <BulletDecorator.Bullet>
                        <Border BorderBrush="#D4D3D3" BorderThickness="2"   Background="White"  Width="14" Height="14" >
                            <Grid>
                                    <Rectangle Name="TickMark" Width="6" Height="6" Fill="#791937" Stroke="#791937" 
                                     Canvas.Top="2.5" Canvas.Left="2.5" StrokeThickness="1" Visibility="Hidden" />
                            </Grid>
                        </Border>
                    </BulletDecorator.Bullet>
                    <ContentPresenter Name="cnt" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                  RecognizesAccessKey="True"
                                  SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                  VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Margin="5,0,0,0"/>
                </BulletDecorator>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsChecked" Value="True">
                        <Setter TargetName="TickMark" Property="Visibility" Value="Visible" />
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter TargetName="TickMark" Property="Opacity" Value="0.5" />
                        <Setter TargetName="cnt" Property="Opacity" Value="0.5"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Thanks for help




Aucun commentaire:

Enregistrer un commentaire