jeudi 29 octobre 2020

WPF - Issue to preserve binding while styling a checkbox with a template

I've an issue while styling a checkbox. I was inspired by the original checkbox style.

The problem is, I can't find a way to apply the original binding (into the property IsSelected). When I remove the style, the binding works. What should I add inside the Content Presenter to preserve the checkbox binding ? Thanks for help.

<DataGridCheckBoxColumn Binding="{Binding IsSelected}" ElementStyle="{StaticResource DataGridCheckBoxStyle}" MinWidth="6">
<Style x:Key="DataGridCheckBoxStyle" TargetType="{x:Type CheckBox}">
    <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="CheckBox">
                    <BulletDecorator Background="Transparent">
                        <BulletDecorator.Bullet>
                            <Border   x:Name="Border"  
                                      Width="13" 
                                      Height="13" 
                                      CornerRadius="0" 
                                      Background="White"
                                      BorderThickness="1"
                                      BorderBrush="{StaticResource Blue}">
                                <Path Width="8" Height="8" 
                                        x:Name="CheckMark"
                                        SnapsToDevicePixels="False" 
                                        Stroke="White"
                                        StrokeThickness="2"
                                        Data="M 0 3.5 L 3.5 7 M 3.5 7 L 8 0" />
                            </Border>
                        </BulletDecorator.Bullet>
                            <ContentPresenter Margin="4,0,0,0"
                                            VerticalAlignment="Center"
                                            HorizontalAlignment="Left"
                                            RecognizesAccessKey="True"/>
                     </BulletDecorator>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsChecked" Value="false">
                            <Setter TargetName="CheckMark" Property="Visibility" Value="Collapsed"/>
                        </Trigger>
                        <Trigger Property="IsChecked" Value="true">
                            <Setter TargetName="Border" Property="Background" Value="{StaticResource Blue}" />
                        </Trigger>
                        <Trigger Property="IsChecked" Value="{x:Null}">
                            <Setter TargetName="CheckMark" Property="Data" Value="M 0 7 L 7 0" />
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="true">
                            <!--<Setter TargetName="Border" Property="Background" Value="{StaticResource DarkGray}" />-->
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter TargetName="Border" Property="Background" Value="White" />
                            <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource LightGray}" />
                            <Setter Property="Foreground" Value="White"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>

    </Style>



Aucun commentaire:

Enregistrer un commentaire