1

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?

4
  • I might be missing something, but the bottom part of your code is working perfectly. If I set BooleanProperty1 = true I see a yellow background and otherwise it's white... (Same for BooleanProperty2 in the other TextBlock) Maybe you're missing INPC? Commented Feb 28, 2013 at 13:55
  • How are you setting the boolean properties? They are part of the dataObject and do implement INPC. When I look at the code at runtime the property is true, but the DataContext of the control shows false. Commented Feb 28, 2013 at 14:23
  • Turns out the problem was that the textblock was inside of a content presenter this meant that when I tried to set the datacontext lower the bindings either didn't work, or remained static. These comments helped answer my problem, but I don't know how to mark that as the answer. Commented Mar 4, 2013 at 14:34
  • The ultimate problem was due to the fact that my textblock was inside of a content presenter which was in a custom control. The custom control was missing code to add its content presenters as logical children so that bindings would work correctly. See the bottom section of this article for information on how to do that. http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/feef9913-9371-4377-a621-c14aa998cc6e/ Commented Mar 6, 2013 at 13:18

1 Answer 1

2

Could always abuse the tag property.

<Window x:Class="WpfApplication2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" DataContext="{Binding RelativeSource={RelativeSource Self}}" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <Style TargetType="{x:Type TextBlock}"> <Style.Triggers> <Trigger Property="Tag" Value="True"> <Setter Property="Background" Value="Yellow"/> <Setter Property="Foreground" Value="Black"/> </Trigger> </Style.Triggers> </Style> </Window.Resources> <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <TextBlock Text="Field1" Grid.Row="0" Tag="{Binding Field1}"/> <TextBlock Text="Field2" Grid.Row="1" Tag="{Binding Field2}"/> </Grid> </Window> 

or if you're feeling adventurous you could create an attached dependency property and pass that over.

public class TextBlockExtender : DependencyObject { public static string GetMyDataField(DependencyObject obj) { return (string)obj.GetValue(MyDataFieldProperty); } public static void SetMyDataField(DependencyObject obj, string value) { obj.SetValue(MyDataFieldProperty, value); } public static readonly DependencyProperty MyDataFieldProperty = DependencyProperty.RegisterAttached("MyDataField", typeof(string), typeof(TextBlockExtender), new PropertyMetadata(null)); } 

used with XAML

<Window x:Class="WpfApplication2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication2" DataContext="{Binding RelativeSource={RelativeSource Self}}" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <Style TargetType="{x:Type TextBlock}"> <Style.Triggers> <Trigger Property="local:TextBlockExtender.MyDataField" Value="True"> <Setter Property="Background" Value="Yellow"/> <Setter Property="Foreground" Value="Black"/> </Trigger> </Style.Triggers> </Style> </Window.Resources> <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <TextBlock Text="Field1" Grid.Row="0" local:TextBlockExtender.MyDataField="{Binding Field1}"/> <TextBlock Text="Field2" Grid.Row="1" local:TextBlockExtender.MyDataField="{Binding Field2}"/> </Grid> </Window> 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.