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