以自定义样式访问依赖项 属性
Accessing a Dependency Property in Custom Style
我需要一些有关依赖属性的帮助。您好,我想访问控件模板中定义的两个属性,CheckedText 和 UncheckedText:
<Style x:Key="ToggleCheckBoxStyle" TargetType="{x:Type CheckBox}">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
<Setter Property="Height" Value="30" />
<Setter Property="Width" Value="110" />
<Setter Property="packages:Variables.X" Value="0" />
<Setter Property="FontFamily" Value="Segoe UI" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="FontSize" Value="13" />
<Setter Property="BorderBrush" Value="#FF939393" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type CheckBox}">
<Grid ClipToBounds="True">
<Grid x:Name="Container">
...
<Border Height="{Binding Height,
RelativeSource={RelativeSource TemplatedParent}}"
HorizontalAlignment="Left"
Background="{TemplateBinding styles:ToggleCheckBox.CheckedBackground}">
<Border.Width>
<MultiBinding Converter="{arithmeticConverter:ArithmeticConverter}" ConverterParameter="x-y">
<Binding Path="Width" RelativeSource="{RelativeSource TemplatedParent}" />
<Binding Path="(styles:ToggleCheckBox.ToggleWidth)" RelativeSource="{RelativeSource TemplatedParent}" />
</MultiBinding>
</Border.Width>
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"
FontFamily="{TemplateBinding FontFamily}"
FontSize="{TemplateBinding FontSize}"
FontWeight="{TemplateBinding FontWeight}"
Foreground="{TemplateBinding styles:ToggleCheckBox.CheckedForeground}"
Text="{TemplateBinding styles:ToggleCheckBox.**CheckedText**}" />
</Border>
<Border Width="{Binding Width,
RelativeSource={RelativeSource TemplatedParent},
Converter={arithmeticConverter:ArithmeticConverter},
ConverterParameter=x-20}"
Height="{Binding Height,
RelativeSource={RelativeSource TemplatedParent}}"
HorizontalAlignment="Left"
Background="{TemplateBinding styles:ToggleCheckBox.UncheckedBackground}">
<Border.RenderTransform>
<TranslateTransform X="{Binding Width, RelativeSource={RelativeSource TemplatedParent}}" />
</Border.RenderTransform>
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"
FontFamily="{TemplateBinding FontFamily}"
FontSize="{TemplateBinding FontSize}"
FontWeight="{TemplateBinding FontWeight}"
Foreground="{TemplateBinding styles:ToggleCheckBox.UncheckedForeground}"
Text="{TemplateBinding styles:ToggleCheckBox.**UncheckedText**}" />
</Border>
</Grid>
<Border Background="Transparent"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="1" />
</Grid>
这里是 ToggleCheckBox 中依赖属性的定义:
public class ToggleCheckBox
{
public static readonly DependencyProperty CheckedTextProperty = DependencyProperty.RegisterAttached("CheckedText", typeof(string), typeof(ToggleCheckBox), new FrameworkPropertyMetadata("ON"));
public static void SetCheckedText(UIElement element, string value)
{
element.SetValue(CheckedTextProperty, value);
}
public static string GetCheckedText(UIElement element)
{
return (String)element.GetValue(CheckedTextProperty);
}
public static readonly DependencyProperty UncheckedTextProperty = DependencyProperty.RegisterAttached("UncheckedText", typeof(string), typeof(ToggleCheckBox), new FrameworkPropertyMetadata("OFF"));
public static void SetUncheckedText(UIElement element, string value)
{
element.SetValue(UncheckedTextProperty, value);
}
public static string GetUncheckedText(UIElement element)
{
return (String)element.GetValue(UncheckedTextProperty);
}
}
而在我看来的用法:
<CheckBox x:Name="IsDataStoreLocal"
HorizontalAlignment="Left"
Style="{StaticResource ToggleCheckBoxStyle}"
CheckedText="YES"
UnCheckedText="NO"/>
我想做的就是上面的事情。当然属性不识别,可能是因为我只有样式,没有控件。如何使用此样式更改新复选框中的 Checked 和 UncheckedText 值?
如有任何帮助,我们将不胜感激。谢谢,威尔
我看过类似的问题,但找不到与我正在尝试做的相匹配的问题。
要在目标元素(复选框)上设置 附加的 属性 值,请使用以下语法:
namespacePrefix:ownerTypeName.propertyName
如果是您的自定义 属性:
<CheckBox styles:ToggleCheckBox.CheckedText="YES"/>
我需要一些有关依赖属性的帮助。您好,我想访问控件模板中定义的两个属性,CheckedText 和 UncheckedText:
<Style x:Key="ToggleCheckBoxStyle" TargetType="{x:Type CheckBox}">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
<Setter Property="Height" Value="30" />
<Setter Property="Width" Value="110" />
<Setter Property="packages:Variables.X" Value="0" />
<Setter Property="FontFamily" Value="Segoe UI" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="FontSize" Value="13" />
<Setter Property="BorderBrush" Value="#FF939393" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type CheckBox}">
<Grid ClipToBounds="True">
<Grid x:Name="Container">
...
<Border Height="{Binding Height,
RelativeSource={RelativeSource TemplatedParent}}"
HorizontalAlignment="Left"
Background="{TemplateBinding styles:ToggleCheckBox.CheckedBackground}">
<Border.Width>
<MultiBinding Converter="{arithmeticConverter:ArithmeticConverter}" ConverterParameter="x-y">
<Binding Path="Width" RelativeSource="{RelativeSource TemplatedParent}" />
<Binding Path="(styles:ToggleCheckBox.ToggleWidth)" RelativeSource="{RelativeSource TemplatedParent}" />
</MultiBinding>
</Border.Width>
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"
FontFamily="{TemplateBinding FontFamily}"
FontSize="{TemplateBinding FontSize}"
FontWeight="{TemplateBinding FontWeight}"
Foreground="{TemplateBinding styles:ToggleCheckBox.CheckedForeground}"
Text="{TemplateBinding styles:ToggleCheckBox.**CheckedText**}" />
</Border>
<Border Width="{Binding Width,
RelativeSource={RelativeSource TemplatedParent},
Converter={arithmeticConverter:ArithmeticConverter},
ConverterParameter=x-20}"
Height="{Binding Height,
RelativeSource={RelativeSource TemplatedParent}}"
HorizontalAlignment="Left"
Background="{TemplateBinding styles:ToggleCheckBox.UncheckedBackground}">
<Border.RenderTransform>
<TranslateTransform X="{Binding Width, RelativeSource={RelativeSource TemplatedParent}}" />
</Border.RenderTransform>
<TextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"
FontFamily="{TemplateBinding FontFamily}"
FontSize="{TemplateBinding FontSize}"
FontWeight="{TemplateBinding FontWeight}"
Foreground="{TemplateBinding styles:ToggleCheckBox.UncheckedForeground}"
Text="{TemplateBinding styles:ToggleCheckBox.**UncheckedText**}" />
</Border>
</Grid>
<Border Background="Transparent"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="1" />
</Grid>
这里是 ToggleCheckBox 中依赖属性的定义:
public class ToggleCheckBox
{
public static readonly DependencyProperty CheckedTextProperty = DependencyProperty.RegisterAttached("CheckedText", typeof(string), typeof(ToggleCheckBox), new FrameworkPropertyMetadata("ON"));
public static void SetCheckedText(UIElement element, string value)
{
element.SetValue(CheckedTextProperty, value);
}
public static string GetCheckedText(UIElement element)
{
return (String)element.GetValue(CheckedTextProperty);
}
public static readonly DependencyProperty UncheckedTextProperty = DependencyProperty.RegisterAttached("UncheckedText", typeof(string), typeof(ToggleCheckBox), new FrameworkPropertyMetadata("OFF"));
public static void SetUncheckedText(UIElement element, string value)
{
element.SetValue(UncheckedTextProperty, value);
}
public static string GetUncheckedText(UIElement element)
{
return (String)element.GetValue(UncheckedTextProperty);
}
}
而在我看来的用法:
<CheckBox x:Name="IsDataStoreLocal"
HorizontalAlignment="Left"
Style="{StaticResource ToggleCheckBoxStyle}"
CheckedText="YES"
UnCheckedText="NO"/>
我想做的就是上面的事情。当然属性不识别,可能是因为我只有样式,没有控件。如何使用此样式更改新复选框中的 Checked 和 UncheckedText 值?
如有任何帮助,我们将不胜感激。谢谢,威尔
我看过类似的问题,但找不到与我正在尝试做的相匹配的问题。
要在目标元素(复选框)上设置 附加的 属性 值,请使用以下语法:
namespacePrefix:ownerTypeName.propertyName
如果是您的自定义 属性:
<CheckBox styles:ToggleCheckBox.CheckedText="YES"/>