title: Silverlight ListboxItem Visibility binding
body: <p>I am making a prototype of a Silverlight application in Expression Blend 4 and I'm trying to show/hide ListBox/ComboBox items based on boolean values in the data they are bound to. I found an example online which suggested this would work but it doesn't:</p>
  
  <pre><code>&lt;UserControl
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing"
      xmlns:local="clr-namespace:PrototypeScreens"
      mc:Ignorable="d"
      x:Class="PrototypeScreens.Toolbar"
      Width="640" Height="31"&gt;
      &lt;UserControl.Resources&gt;
          &lt;local:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" /&gt;
      &lt;/UserControl.Resources&gt;
  
      &lt;Grid x:Name="LayoutRoot" Background="White"&gt;
          &lt;ComboBox HorizontalAlignment="Right" Style="{StaticResource ComboBox-Sketch}" VerticalAlignment="Top" Width="32" ItemsSource="{Binding Sites}" &gt;
              &lt;ComboBox.ItemTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;Grid&gt;
                      &lt;ed:RegularPolygon Fill="{StaticResource BaseBackground-Sketch}" ed:GeometryEffect.GeometryEffect="Sketch" HorizontalAlignment="Left" Height="10" InnerRadius="0.47211" Margin="0,0,0,0" PointCount="5" Stretch="Fill" Stroke="{StaticResource BaseBorder-Sketch}" StrokeThickness="2" UseLayoutRounding="False" Width="10"
                          Visibility="{Binding IsFavorite, Converter={StaticResource BooleanToVisibilityConverter}}" /&gt;                   
                      &lt;TextBlock Style="{StaticResource BasicTextBlock-Sketch}" Text="{Binding Name}" HorizontalAlignment="Left" Margin="15,0,0,0" /&gt;                     
                      &lt;/Grid&gt;
                  &lt;/DataTemplate&gt;
              &lt;/ComboBox.ItemTemplate&gt;
              &lt;!--
              &lt;ComboBox.ItemContainerStyle&gt;
                  &lt;Style TargetType="ComboBoxItem"&gt;
                      &lt;Setter Property="Visibility" Value="{Binding IsRegistered, Converter={StaticResource BooleanToVisibilityConverter}}" /&gt;
                  &lt;/Style&gt;
              &lt;/ComboBox.ItemContainerStyle&gt;
              --&gt;
          &lt;/ComboBox&gt;
          &lt;ListBox Height="29" Margin="0,0,32,0" Style="{StaticResource ListBox-Sketch}" VerticalAlignment="Top" ItemsSource="{Binding Sites}" ScrollViewer.HorizontalScrollBarVisibility="Hidden"&gt;
              &lt;ListBox.ItemsPanel&gt;
                  &lt;ItemsPanelTemplate&gt;
                      &lt;StackPanel Orientation="Horizontal" FlowDirection="RightToLeft" /&gt;
                  &lt;/ItemsPanelTemplate&gt;
              &lt;/ListBox.ItemsPanel&gt;
              &lt;ListBox.ItemTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;Grid&gt;
                      &lt;TextBlock Style="{StaticResource BasicTextBlock-Sketch}" Text="{Binding Id}" HorizontalAlignment="Left" Margin="0,0,0,0" /&gt;                        
                      &lt;ed:RegularPolygon Fill="{StaticResource BaseBackground-Sketch}" ed:GeometryEffect.GeometryEffect="Sketch" HorizontalAlignment="Left" Height="10" InnerRadius="0.47211" Margin="20,0,0,0" PointCount="5" Stretch="Fill" Stroke="{StaticResource BaseBorder-Sketch}" StrokeThickness="2" UseLayoutRounding="False" Width="10"/&gt;                  
                      &lt;/Grid&gt;
                  &lt;/DataTemplate&gt;
              &lt;/ListBox.ItemTemplate&gt;
              &lt;!--
              &lt;ListBox.ItemContainerStyle&gt;
                  &lt;Style TargetType="ListBoxItem"&gt;
                      &lt;Setter Property="Visibility" Value="{Binding IsFavorite, Converter={StaticResource BooleanToVisibilityConverter}}" /&gt;
                  &lt;/Style&gt;
              &lt;/ListBox.ItemContainerStyle&gt;
              --&gt;
          &lt;/ListBox&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>When I try to load the screen with this user control, the app crashes unless I comment out the container styles (as in above code). What am I doing wrong?</p>
  
  <p><strong>NOTE:</strong> The visibility of the polygon inside the ComboBoxItem works fine.</p>
  
  <p><strong>UPDATE</strong> I included the xaml for the entire usercontrol</p>
  
  <p><strong>UPDATE</strong> I got some error details:</p>
  
  <pre><code>Webpage error details
  
  User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; MDDC; .NET4.0C; .NET4.0E; BRI/2)
  Timestamp: Tue, 10 May 2011 15:21:02 UTC
  
  
  Message: Unhandled Error in Silverlight Application Exception has been thrown by the target of an invocation.   at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean&amp; canBeCached, RuntimeMethodHandleInternal&amp; ctor, Boolean&amp; bNeedSecurityCheck)
     at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
     at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
     at System.Activator.CreateInstance(Type type, Boolean nonPublic)
     at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
     at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
     at System.Reflection.Assembly.CreateInstance(String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
     at Microsoft.Expression.Prototyping.Workspace.PlayerWindow.InstantiateScreen(String screen, Boolean showImmediately)
     at Microsoft.Expression.Prototyping.Workspace.PlayerWindow.TransitionScreens(String from, String to)
     at Microsoft.Expression.Prototyping.Navigation.NavigationViewModel.NavigateToScreen(String name, Boolean record)
  Line: 1
  Char: 1
  Code: 0
  URI: file:///D:/Projects/Expression/Prototype/Bin/Debug/Default.html
  </code></pre>
  
  <p><strong>UPDATE:</strong> I think I made some progress. It looks like the binding can not be resolved. The ListBox and ComboBox have both <code>ItemsSource="{Binding Sites}"</code> and <code>Sites</code> is of type <code>System.Collections.ObjectModel.ObservableCollection&lt;SitesItem&gt;</code>. <code>SiteItem</code> has properties <code>IsRegistered</code> and <code>IsFavorite</code>. </p>
  
  <p>So I guess the question is: Is it possible to bind to bind to <code>IsRegistered</code> and <code>IsFavorite</code> in a <code>ItemContainerStyle</code>?</p>