gpt4 book ai didi

wpf - 整合常见的 WPF 样式

转载 作者:行者123 更新时间:2023-12-03 02:57:30 26 4
gpt4 key购买 nike

我的 WPF XAML 中有各种 Style 元素,除了数据绑定(bind)属性之外,这些元素都是相同的,例如:

<Style x:Key="HasAlphaStyle" TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=HasAlpha, UpdateSourceTrigger=PropertyChanged}" Value="True">
<Setter Property="Background" Value="Red"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="FontWeight" Value="Bold"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=HasAlpha, UpdateSourceTrigger=PropertyChanged}" Value="False">
<Setter Property="Background" Value="LightGreen"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="FontWeight" Value="Normal"/>
</DataTrigger>
</Style.Triggers>
</Style>

<Style x:Key="HasBetaStyle" TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=HasBeta, UpdateSourceTrigger=PropertyChanged}" Value="True">
<Setter Property="Background" Value="Red"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="FontWeight" Value="Bold"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=HasBeta, UpdateSourceTrigger=PropertyChanged}" Value="False">
<Setter Property="Background" Value="LightGreen"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="FontWeight" Value="Normal"/>
</DataTrigger>
</Style.Triggers>
</Style>

该样式应用于如下控件:

<TextBlock Style="{StaticResource HasAlphaStyle}" .../>

有没有办法可以合并 HasAlphaStyle 和 HasBetaStyle,以便不必重复属性 setter ?两者之间的唯一区别是属性的绑定(bind)路径。

最佳答案

我将创建一个附加属性并在其上放置触发器,而不是使用数据触发器。示例代码如下:

附属属性(property)

public static class TextBlockBehavior
{
public static readonly DependencyProperty HasValueProperty =
DependencyProperty.RegisterAttached("HasValue", typeof(bool), typeof(TextBlockBehavior),
new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.None));

public static void SetHasValue(DependencyObject depObject, bool value)
{
depObject.SetValue(HasValueProperty, value);
}

public static bool GetHasValue(DependencyObject depObject)
{
return (bool)depObject.GetValue(HasValueProperty);
}
}

然后你的组合风格就会变成

<Style x:Key="HasValueStyle" TargetType="TextBlock">
<Style.Triggers>
<Trigger Property="behaviors:TextBlockBehavior.HasValue" Value="True">
<Setter Property="Background" Value="Red"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="FontWeight" Value="Bold"/>
</Trigger>
<Trigger Property="behaviors:TextBlockBehavior.HasValue" Value="False">
<Setter Property="Background" Value="LightGreen"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="FontWeight" Value="Normal"/>
</Trigger>
</Style.Triggers>
</Style>

您可以将文本 block 写为

<TextBlock Style="{StaticResource HasValueStyle}"
behaviors:TextBlockBehavior.HasValue="{Binding Path=HasAlpha, UpdateSourceTrigger=PropertyChanged}" .../>

<TextBlock Style="{StaticResource HasValueStyle}"
behaviors:TextBlockBehavior.HasValue="{Binding Path=HasBeta, UpdateSourceTrigger=PropertyChanged}" .../>

关于wpf - 整合常见的 WPF 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15814639/

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