我的按钮有一个标准样式,但我希望样式的某些部分是可配置的。例如当按钮触发MouseOver时,我出现了边框,我希望边框颜色可配置。
关注这篇文章:http://www.thomaslevesque.com/2011/10/01/wpf-creating-parameterized-styles-with-attached-properties/我以为我可以使用附加属性和TemplateBinding来实现这一目标。
我创建了以下附加属性:
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>
据我所知,这应该可行,但我在上一行的运行时遇到以下错误:
无法将属性“Value”中的值转换为“'类型的对象。 标记文件
中对象'System.Windows.Setter'出错
我在这里做错了吗?我是WPF的新手,无法弄清楚出了什么问题,因为附加属性的类型是一个Brush,这是我期望Border的BorderBrush属性所需要的。
答案 0 :(得分:44)
我认为在编译时会对TemplateBinding进行评估,因此您无法在Setter中动态设置TemplateBinding,请尝试使用Binding(参见下文)。
<Setter TargetName="border" Property="BorderBrush"
Value="{Binding Path=(local:ThemeProperties.ButtonBorderColour),
RelativeSource={RelativeSource TemplatedParent}}"/>
希望这有帮助。
答案 1 :(得分:0)
试试这个:
<Setter TargetName="border" Property="BorderBrush" Value="{TemplateBinding Path=(local:ThemeProperties.ButtonBorderColour)}" />
区别在于属性周围的括号表示它是附属属性。