I am trying to create a style for a textblock to create a highlight effect. This style will be used on a number of TextBlocks, each bound to a different property. The datacontext for my main control is set in code behind:
this.DataContext = dataobject; I can create one textblock with a working binding like this:
<TextBlock Text="Field1"> <TextBlock.Style> <Style x:Key="HighlightStyle" TargetType="{x:Type TextBlock}"> <Style.Triggers> <DataTrigger Binding="{Binding Path=.}" Value="True"> <Setter Property="Background" Value="Yellow"/> <Setter Property="Foreground" Value="Black"/> </DataTrigger> </Style.Triggers> </Style> </TextBlock.Style> <TextBlock> But I need to change the binding so that the style can be used on different TextBlocks. Something like:
<Style x:Key="HighlightStyle" TargetType="{x:Type TextBlock}"> <Style.Triggers> <DataTrigger Binding="{Binding Path=.}" Value="True"> <Setter Property="Background" Value="Yellow"/> <Setter Property="Foreground" Value="Black"/> </DataTrigger> </Style.Triggers> </Style> <TextBlock Text="Field1" DataContext="{Binding Path=BooleanProperty1}" Style="{StaticResource HighlightStyle}"/> <TextBlock Text="Field2" DataContext="{Binding Path=BooleanProperty2}" Style="{StaticResource HighlightStyle}"/> When I try this, changing the properties does nothing to highlight the textblock. Is there a way to accomplish this?
BooleanProperty1 = trueI see a yellow background and otherwise it's white... (Same for BooleanProperty2 in the other TextBlock) Maybe you're missing INPC?