6

我的 WPF XAML 中有各种 Style 元素,除了数据绑定属性外,它们是相同的,例如:

<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 以便不必复制属性设置器?两者之间的唯一区别是属性的绑定路径。

4

1 回答 1

3

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

附加属性

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>

你可以把你的文本块写成

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

<TextBlock Style="{StaticResource HasValueStyle}"
           behaviors:TextBlockBehavior.HasValue="{Binding Path=HasBeta, UpdateSourceTrigger=PropertyChanged}"            .../>
于 2013-04-04T16:15:49.900 回答