带有附加属性的模板绑定 [英] Template Binding with Attached Properties
问题描述
我的按钮有标准样式,但我希望样式的某些部分是可配置的.例如当为按钮触发 MouseOver 时,我有一个边框出现,我希望边框颜色是可配置的.
I have a standard style for my buttons but I want certain parts of the style to be configurable. e.g. I have a border appear when MouseOver is triggered for the button and I want the border colour to be configurable.
按照这篇文章:http://www.thomaslevesque.com/2011/10/01/wpf-creating-parameterized-styles-with-attached-properties/ 我想我可以使用附加属性和模板绑定来实现这一点.
Following this article: http://www.thomaslevesque.com/2011/10/01/wpf-creating-parameterized-styles-with-attached-properties/ I thought I could use attached properties and TemplateBinding to achieve this.
我创建了以下附加属性:
I created the following attached property:
public static class ThemeProperties
{
public static Brush GetButtonBorderColour(DependencyObject obj)
{
return (Brush)obj.GetValue(ButtonBorderColourProperty);
}
public static void SetButtonBorderColour(DependencyObject obj, Brush value)
{
obj.SetValue(ButtonBorderColourProperty, value);
}
public static readonly DependencyProperty ButtonBorderColourProperty =
DependencyProperty.RegisterAttached(
"ButtonBorderColour",
typeof(Brush),
typeof(ThemeProperties),
new FrameworkPropertyMetadata(Brushes.Black, FrameworkPropertyMetadataOptions.Inherits));
}
我这样设置属性:
<Button Style="{StaticResource RedButton}" local:ThemeProperties.ButtonBorderColour="#B20000"/>
我的风格是这样的:
<Window.Resources>
<Style x:Key="RedButton" TargetType="Button">
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Margin" Value="2"/>
<Setter Property="FontFamily" Value="Tahoma"/>
<Setter Property="FontSize" Value="11px"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="MinHeight" Value="25" />
<Setter Property="FocusVisualStyle" Value="{StaticResource MyFocusVisual}" />
<Setter Property="Background" >
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1" >
<GradientStop Color="#FECCBF" Offset="0.2"/>
<GradientStop Color="Red" Offset="0.85"/>
<GradientStop Color="#FECCBF" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border Name="border" BorderThickness="1" Padding="4,2" BorderBrush="Transparent" CornerRadius="3" Background="{TemplateBinding Background}">
<Grid >
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Name="content"/>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="border" Property="BorderBrush" Value="{TemplateBinding local:ThemeProperties.ButtonBorderColour}" />
<Setter Property="Foreground" Value="#B20000" />
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" >
<Setter.Value>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1" >
<GradientStop Color="#FECCBF" Offset="0.35"/>
<GradientStop Color="Red" Offset="0.95"/>
<GradientStop Color="#FECCBF" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
<Setter TargetName="content" Property="RenderTransform" >
<Setter.Value>
<TranslateTransform Y="1.0" />
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsDefaulted" Value="True">
<Setter TargetName="border" Property="BorderBrush" Value="#B20000" />
</Trigger>
<Trigger Property="IsFocused" Value="True">
<Setter TargetName="border" Property="BorderBrush" Value="#B20000" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="border" Property="Opacity" Value="0.7" />
<Setter Property="Foreground" Value="Gray" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
关键线在哪里
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="border" Property="BorderBrush" Value="{TemplateBinding local:ThemeProperties.ButtonBorderColour}" />
<Setter Property="Foreground" Value="#B20000" />
</Trigger>
据我所知,这应该可行,但在运行时出现以下错误:
As far as I can see this should work but I get the following error during runtime on the above line:
无法将属性 'Value' 中的值转换为类型 '' 的对象.标记文件中的对象System.Windows.Setter"出错
Cannot convert the value in attribute 'Value' to object of type ''. Error at object 'System.Windows.Setter' in markup file
我在这里做错了什么吗?我是 WPF 的新手,无法弄清楚出了什么问题,因为附加属性的类型是 Brush,这是我期望 Border 的 BorderBrush 属性想要的.
Have I done something incorrect here? I'm brand new to WPF and can't figure what's going wrong as the Type of the attached property is a Brush which is what I would expect the BorderBrush property of a Border to want.
推荐答案
我认为 TemplateBinding 是在编译时评估的,因此您无法在 Setter 中动态设置 TemplateBinding,请尝试改用 Binding(见下文).
I think TemplateBinding is evaluated at compile time so you can't dynamically set a TemplateBinding in your Setter, try using Binding instead (see below).
<Setter TargetName="border" Property="BorderBrush"
Value="{Binding Path=(local:ThemeProperties.ButtonBorderColour),
RelativeSource={RelativeSource TemplatedParent}}"/>
希望这会有所帮助.
这篇关于带有附加属性的模板绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!