Reputation: 1447
I use a datatrigger to change checkbox Image Source to icon.
<CheckBox Content="An Image CheckBox Label" Foreground="Black" Margin="73,103,27,142" IsThreeState="True" >
<CheckBox.Template>
<ControlTemplate TargetType="{x:Type CheckBox}">
<StackPanel Orientation="Horizontal">
<Image>
<Image.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked, RelativeSource={RelativeSource AncestorType={x:Type CheckBox}}}" Value="True">
<Setter Property="Image.Source" Value="/Images/Checkbox.ico" />
</DataTrigger>
<DataTrigger Binding="{Binding IsChecked, RelativeSource={RelativeSource AncestorType={x:Type CheckBox}}}" Value="False">
<Setter Property="Image.Source" Value="/Images/CheckboxUncheck.ico" />
</DataTrigger>
<DataTrigger Binding="{Binding IsChecked, RelativeSource={RelativeSource AncestorType={x:Type CheckBox}}}" Value="{x:Null}">
<Setter Property="Image.Source" Value="/Images/delete_12x12.png" />
<Setter Property="Image.Width" Value="10" />
<Setter Property="Image.Height" Value="10" />
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
<ContentPresenter Content="{TemplateBinding Content}" Margin="5,0,0,0" />
</StackPanel>
</ControlTemplate>
</CheckBox.Template>
</CheckBox>
Is it any way to set checkbox style(or image) to the default for states when isChecked=False Value="False
or inderterminate Value="{x:Null}
?
Upvotes: 0
Views: 474
Reputation: 169150
If you only want to apply your custom template when the CheckBox
is checked you could apply a Style
with a Trigger
to the CheckBox
:
<CheckBox Content="An Image CheckBox Label" Foreground="Black" Margin="73,103,27,142" IsThreeState="True" >
<CheckBox.Style>
<Style TargetType="CheckBox">
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type CheckBox}">
<StackPanel Orientation="Horizontal">
<Image Source="/Images/Checkbox.ico" />
<ContentPresenter Content="{TemplateBinding Content}" Margin="5,0,0,0" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</CheckBox.Style>
</CheckBox>
Upvotes: 1