title: Set Datacontext of a control to a property
body: <p>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:</p>
  
  <pre><code>this.DataContext = dataobject;
  </code></pre>
  
  <p>I can create one textblock with a working binding like this:</p>
  
  <pre><code>&lt;TextBlock Text="Field1"&gt;
      &lt;TextBlock.Style&gt;
          &lt;Style x:Key="HighlightStyle" TargetType="{x:Type TextBlock}"&gt;
              &lt;Style.Triggers&gt;
                  &lt;DataTrigger Binding="{Binding Path=.}" Value="True"&gt;
                      &lt;Setter Property="Background" Value="Yellow"/&gt;
                      &lt;Setter Property="Foreground" Value="Black"/&gt;
                   &lt;/DataTrigger&gt;
              &lt;/Style.Triggers&gt;
          &lt;/Style&gt;
      &lt;/TextBlock.Style&gt;
  &lt;TextBlock&gt;
  </code></pre>
  
  <p>But I need to change the binding so that the style can be used on different TextBlocks. Something like:</p>
  
  <pre><code>&lt;Style x:Key="HighlightStyle" TargetType="{x:Type TextBlock}"&gt;
      &lt;Style.Triggers&gt;
          &lt;DataTrigger Binding="{Binding Path=.}" Value="True"&gt;
              &lt;Setter Property="Background" Value="Yellow"/&gt;
              &lt;Setter Property="Foreground" Value="Black"/&gt;
          &lt;/DataTrigger&gt;
      &lt;/Style.Triggers&gt;
  &lt;/Style&gt;
  
  &lt;TextBlock Text="Field1" DataContext="{Binding Path=BooleanProperty1}" Style="{StaticResource HighlightStyle}"/&gt;
  &lt;TextBlock Text="Field2" DataContext="{Binding Path=BooleanProperty2}" Style="{StaticResource HighlightStyle}"/&gt;
  </code></pre>
  
  <p>When I try this, changing the properties does nothing to highlight the textblock. Is there a way to accomplish this?</p>