I would create an attached property and enable triggers instead of data triggers. Sample code below:
Attached 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); } }
and then your combined style will become
<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>
and you can write your text blocks as
<TextBlock Style="{StaticResource HasValueStyle}" behaviors:TextBlockBehavior.HasValue="{Binding Path=HasAlpha, UpdateSourceTrigger=PropertyChanged}" .../> <TextBlock Style="{StaticResource HasValueStyle}" behaviors:TextBlockBehavior.HasValue="{Binding Path=HasBeta, UpdateSourceTrigger=PropertyChanged}" .../>
source share