Combining common WPF styles

I have different style elements in my WPF XAML that are the same except for the data binding property, for example:

<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> 

The style is applied to the control, for example:

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

Is there a way to combine my HasAlphaStyle and HasBetaStyle so as not to duplicate the properties? The only difference between the two is the path to the property.

+6
source share
1 answer

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}" .../> 
+3
source

All Articles