title: Setting style based on existence of an ancestor type
body: <p>I have 2 sets of <code>TextBlocks</code> some of them are in an <code>ItemControl</code> and some of them are not.</p>
  <p>I want to make a style (just based on type) which sets the background of the <code>TextBlock</code> if its ancestor is an <code>ItemControl</code>.</p>
  <p>I can do it using the following code but my problem is that on the log (and output window) a data biding error message is displayed because of the <code>TextBlocks</code> which do not have <code>ItemControl</code> as their ancestor.</p>
  <p>Is there a better way to do this task and avoid this error message?</p>
  <pre class="lang-xml prettyprint-override"><code>&lt;Grid&gt;
      &lt;Grid.Resources&gt;
          &lt;local:HasAncestorConverter x:Key=&quot;HasAncestorConverter&quot; /&gt;
          &lt;Style TargetType=&quot;TextBlock&quot;&gt;            
              &lt;Style.Triggers&gt;
                  &lt;DataTrigger
                      Binding=&quot;{Binding RelativeSource={RelativeSource
                      AncestorType={x:Type ItemsControl}},
                      Converter={StaticResource HasAncestorConverter}}&quot; Value=&quot;True&quot;&gt;
                      &lt;Setter Property=&quot;Background&quot;
                              Value=&quot;{Binding Tag,
                              RelativeSource={RelativeSource
                              AncestorType={x:Type ItemsControl}}}&quot; /&gt;
                  &lt;/DataTrigger&gt;
              &lt;/Style.Triggers&gt;
          &lt;/Style&gt;
      &lt;/Grid.Resources&gt;
      &lt;StackPanel&gt;
          &lt;TextBlock Text=&quot;Out of ItemControl&quot; /&gt;
          &lt;ItemsControl Tag=&quot;Blue&quot; &gt;
              &lt;TextBlock Text=&quot;Inside of ItemControl&quot; /&gt;
          &lt;/ItemsControl&gt;
      &lt;/StackPanel&gt;
  &lt;/Grid&gt;    
  </code></pre>
  <p>Convertor:</p>
  <pre class="lang-cs prettyprint-override"><code>class HasAncestorConverter : IValueConverter
  {
      public object Convert(object value, Type targetType, object parameter,
          System.Globalization.CultureInfo culture)
      {
          return value != null;
      }
      public object ConvertBack(object value, Type targetType, object parameter,
          System.Globalization.CultureInfo culture)
      {
          throw new NotImplementedException();
      }
  }
  </code></pre>
  <p>Error message:</p>
  <blockquote>
  <p>System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=; DataItem=null; target element is 'TextBlock' (Name=''); target property is 'NoTarget' (type 'Object')</p>
  </blockquote>