title: Show XAML on Condition
body: <p>The context is I have a Listbox that is outputting items with quite a lot of styling (more than i've put here)... I went to use a DataTemplateSelector to show either one image or another based on a condition but it was stupid to have to re-write the whole tempalte with just this one difference in it.</p>
  
  <p>I have placed some pseudo code to demonstrate what i'm trying to achieve:</p>
  
  <pre><code>&lt;ListBox Grid.Row="1"
              ItemsSource="{Binding TestCases}"
              BorderThickness="0"
              HorizontalAlignment="Stretch"
              HorizontalContentAlignment="Stretch"&gt;
      &lt;ListBox.ItemTemplate&gt;
          &lt;DataTemplate&gt;
              &lt;Grid HorizontalAlignment="Stretch"&gt;
                  &lt;Grid.ColumnDefinitions&gt;
                      &lt;ColumnDefinition Width="*"&gt;&lt;/ColumnDefinition&gt;
                      &lt;ColumnDefinition Width="auto"&gt;&lt;/ColumnDefinition&gt;
                      &lt;ColumnDefinition Width="10"&gt;&lt;/ColumnDefinition&gt;
                  &lt;/Grid.ColumnDefinitions&gt;
  
                  &lt;TextBlock Grid.Column="0"
                              Padding="0,5,0,5"&gt;
                      &lt;Run Text="{Binding Class}"&gt;&lt;/Run&gt;
                  &lt;/TextBlock&gt;
  
                  &lt;TextBlock Grid.Column="1"
                              Padding="5"&gt;
                      &lt;Run Text="{Binding Assertions}"&gt;&lt;/Run&gt;
                  &lt;/TextBlock&gt;
  
  if {Binding Failures} &gt; 0 {
  
      &lt;Image HorizontalAlignment="Center"
              Width="10"
              Height="10"
              Grid.Column="2"
              Source="/Content/Images/Cross.png"&gt;
      &lt;/Image&gt;
  
  }
  else
  {
  
      &lt;Image HorizontalAlignment="Center"
              Width="10"
              Height="10"
              Grid.Column="2"
              Source="/Content/Images/Tick.png"&gt;
      &lt;/Image&gt;
  
  }
  
              &lt;/Grid&gt;
          &lt;/DataTemplate&gt;
      &lt;/ListBox.ItemTemplate&gt;
  &lt;/ListBox&gt;
  </code></pre>
  
  <p>Any ideas on how i do it?</p>
  
  <p>-------------Edit------------</p>
  
  <p>So far i've added the namespace of my converter class to the XAML file:</p>
  
  <pre><code>xmlns:converters="clr-namespace:Blah.Blah.Converters"
  </code></pre>
  
  <p>Then added window resources:</p>
  
  <pre><code>&lt;Window.Resources&gt;
      &lt;converters:FailuresTickConverter x:Key="failuresTickConverter" /&gt;
      &lt;converters:FailuresCrossConverter x:Key="failuresCrossConverter" /&gt;
  &lt;/Window.Resources&gt;
  </code></pre>
  
  <p>Then i have the classes themselves:</p>
  
  <pre><code>namespace Blah.Blah.Converters
  {
      public class FailuresTickConverter : IValueConverter
      {
          public object Convert( object value, Type targetType,
              object parameter, CultureInfo culture )
          {
              int failures = (int)value;
  
              if( failures &gt; 0 )
                  return Visibility.Hidden;
              return Visibility.Visible;
          }
  
          public object ConvertBack( object value, Type targetType,
              object parameter, CultureInfo culture )
          {
              throw new NotImplementedException();
          }
      }
  
      public class FailuresCrossConverter : IValueConverter
      {
          public object Convert( object value, Type targetType,
              object parameter, CultureInfo culture )
          {
              int failures = ( int )value;
  
              if( failures &gt; 0 )
                  return Visibility.Visible;
              return Visibility.Hidden;
          }
  
          public object ConvertBack( object value, Type targetType,
              object parameter, CultureInfo culture )
          {
              throw new NotImplementedException();
          }
      }
  }
  </code></pre>
  
  <p>Then in my XAML on the images i've done this:</p>
  
  <pre><code>&lt;Image Visibility="{Binding Failures, Converter=failuresTickConverter}"
          HorizontalAlignment="Center"
          Width="10"
          Height="10"
          Grid.Column="2"
          Grid.Row="0"
          Source="/Content/Images/Tick.png"&gt;
  &lt;/Image&gt;
  
  &lt;Image Visibility="{Binding Failures, Converter=failuresCrossConverter}"
          HorizontalAlignment="Center"
          Width="10"
          Height="10"
          Grid.Column="2"
          Grid.Row="0"
          Source="/Content/Images/Cross.png"&gt;
  &lt;/Image&gt;
  </code></pre>
  
  <p>I'm getting an error on the bindings:</p>
  
  <blockquote>
    <p>IValueConverter does not support converting from string</p>
  </blockquote>