我找到解决方案Setter.TargetName does not work against an element from the BasedOn setting。 但我不明白他是如何向我起诉的。 我有一节课:
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>
也许还有另一种解决方案
答案 0 :(得分:1)
除非您出于其他原因需要延长CheckBox
,否则您在此处尝试执行的操作也可以通过附加属性完成,并且您可以使用一个Style
你所有的CheckBoxes。
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
以及用于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>
编辑:即使您仍需要展开{{1}},此样式也适用于常规CheckBox
和CheckBox
实施。只需创建另一个基于此的Style。
MyCheckBox
如果您想从代码隐藏中访问<Style TargetType="{x:Type My:MyCheckBox}" BasedOn="{StaticResource {x:Type CheckBox}}">
...
</Style>
属性,可以这样做:
DefaultValue