gpt4 book ai didi

wpf - 带有附加属性的模板绑定(bind)

转载 作者:行者123 更新时间:2023-12-03 11:34:56 25 4
gpt4 key购买 nike

我的按钮有一个标准样式,但我希望样式的某些部分是可配置的。例如当为按钮触发 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>

据我所见,这应该可以工作,但是在运行时在上面的行中出现以下错误:

Cannot convert the value in attribute 'Value' to object of type ''. Error at object 'System.Windows.Setter' in markup file



我在这里做错了什么吗?我是 WPF 的新手,不知道出了什么问题,因为附加属性的类型是 Brush,这是我希望 BorderBrush 属性想要的。

最佳答案

我认为 TemplateBinding 是在编译时评估的,因此您不能在 Setter 中动态设置 TemplateBinding,请尝试改用 Binding(见下文)。

<Setter TargetName="border" Property="BorderBrush" 
Value="{Binding Path=(local:ThemeProperties.ButtonBorderColour),
RelativeSource={RelativeSource TemplatedParent}}"/>

希望这可以帮助。

关于wpf - 带有附加属性的模板绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9232502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com