合并常见的WPF样式

时间:2013-04-04 14:51:39

标签: wpf xaml styles wpf-4.0

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

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

1 个答案:

答案 0 :(得分: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}"            .../>