在复选框中更改默认位图
本文关键字:默认 位图 复选框 | 更新日期: 2023-09-27 17:57:00
我发现解决方案Setter.TargetName不适用于BasedOn设置中的元素。但我不明白他对我有多好。我有一堂课:
public class MyCheckBox: CheckBox
{
public bool DefaultValue
{
get { return (bool)GetValue(DefaultValueProperty); }
set { SetValue(DefaultValueProperty, value); }
}
public static readonly DependencyProperty DefaultValueProperty =
DependencyProperty.Register("DefaultValue", typeof(bool), typeof(MyCheckBox), new UIPropertyMetadata(false));
protected override void OnToggle()
{
this.IsChecked = this.IsChecked == null ? !DefaultValue : this.IsChecked.Value ? false : true;
}
}
和 XAML
<Style TargetType="{x:Type CheckBox}">
<Style.Resources>
<Imaging:BitmapImage x:Key="CheckBoxStatusSource" UriSource="FalseIfNull.png"/> <!-- icon if [IsChecked] is [null] -->
</Style.Resources>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type CheckBox}">
<Image x:Name="CheckBoxStatus" Source="{DynamicResource CheckBoxStatusSource}"/>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="CheckBoxStatus" Property="Source" Value="b.png"/><!-- icon if [IsChecked] is [true] -->
</Trigger>
<Trigger Property="IsChecked" Value="False">
<Setter TargetName="CheckBoxStatus" Property="Source" Value="c.png"/><!-- icon if [IsChecked] is [false] -->
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type My:MyCheckBox}" BasedOn="{StaticResource {x:Type CheckBox}}">
<Style.Triggers>
<Trigger Property="DefaultValue" Value="true">
<!-- !!!!!!!!!!!!!!This need change [UriSource] in [CheckBoxStatusSource] to "TrueIfNull.png"!!!!!!!!!!!!!! -->
</Trigger>
</Style.Triggers>
</Style>
也许还有另一种解决方案
除非您出于其他原因需要扩展CheckBox
,否则您在此处尝试执行的操作也可以使用附加属性来完成,并且它将允许您为所有复选框提供一个Style
。
public static class CheckBoxExtensions
{
public static void SetDefaultValue(CheckBox element, bool value)
{
element.SetValue(DefaultValueProperty, value);
}
public static bool GetDefaultValue(CheckBox element)
{
return (bool)element.GetValue(DefaultValueProperty);
}
// Using a DependencyProperty as the backing store for DefaultValue. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DefaultValueProperty =
DependencyProperty.RegisterAttached("DefaultValue", typeof(bool), typeof(CheckBoxExtensions), new UIPropertyMetadata(false));
}
此属性现在可供您在解决方案中的任何CheckBox
以及用于复选框的任何样式和模板中使用。在这种情况下...
<Style TargetType="{x:Type CheckBox}">
<Style.Resources>
<Imaging:BitmapImage x:Key="CheckBoxStatusSourceFalse" UriSource="FalseIfNull.png"/> <!-- icon if [IsChecked] is [null] and [DefaultValue] is [false] -->
<Imaging:BitmapImage x:Key="CheckBoxStatusSourceTrue" UriSource="TrueIfNull.png"/> <!-- icon if [IsChecked] is [null] and [DefaultValue] is [true] -->
</Style.Resources>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type CheckBox}">
<Image x:Name="CheckBoxStatus" Source="{DynamicResource CheckBoxStatusSourceFalse}"/>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="local:CheckBoxExtensions.DefaultValue" Value="True" />
<Condition Property="IsChecked" Value="{x:Null}" />
</MultiTrigger.Conditions>
<Setter TargetName="CheckBoxStatus" Property="Source" Value="{DynamicResource CheckBoxStatusSourceFalse}"/>
</MultiTrigger>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="CheckBoxStatus" Property="Source" Value="b.png"/><!-- icon if [IsChecked] is [true] -->
</Trigger>
<Trigger Property="IsChecked" Value="False">
<Setter TargetName="CheckBoxStatus" Property="Source" Value="c.png"/><!-- icon if [IsChecked] is [false] -->
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
编辑:即使你仍然需要扩展CheckBox
,这种风格也适用于常规CheckBox
和你的MyCheckBox
实现。只需创建基于此样式的另一个样式即可。
<Style TargetType="{x:Type My:MyCheckBox}" BasedOn="{StaticResource {x:Type CheckBox}}">
...
</Style>
如果你想从代码隐藏访问DefaultValue
属性,你可以这样做:
CheckBoxExtensions.SetDefaultValue(checkBox, true);
bool defaultValue = CheckBoxExtensions.GetDefaultValue(checkBox);