title: WPF Binding to ListView SelectedItem Is Not Working
body: <p>I'm using a ListView to display contents of a log in my application.  I want to change the icon and visibility of a context MenuItem based upon the user's currently selected entry in the ListView.</p>
  
  <p>Here is how I'm populating the ListView:</p>
  
  <pre><code>// Create the collection view source.
  m_CollectionViewSource = new CollectionViewSource();                
  m_CollectionViewSource.SortDescriptions.Add(new System.ComponentModel.SortDescription("Time", System.ComponentModel.ListSortDirection.Descending));
  m_CollectionViewSource.Filter += new FilterEventHandler(LogEventCollectionViewSource_Filter);
  
  // Create the binding.
  Binding binding = new Binding();
  binding.Source = m_CollectionViewSource;
  this.LogEventListView.SetBinding(ListView.ItemsSourceProperty, binding);
  m_CollectionViewSource.Source = LogEventManager.Instance.LogEventCollection;
  </code></pre>
  
  <p>And here is where I'm creating my ListView control.</p>
  
  <pre><code>    &lt;ListView x:Name="LogEventListView" 
            Grid.Row="0"
            Grid.Column="0"
            SelectionMode="Single"
            VirtualizingStackPanel.IsVirtualizing="True"&gt;
      &lt;ListView.ContextMenu&gt;
          &lt;ContextMenu Opened="ContextMenu_Opened"&gt;
              &lt;MenuItem x:Name="ContextMenuViewDetails"
                        Header="View Details..."
                        ToolTip="Shows all of the data associated with the log event message."
                        Visibility="{Binding ElementName=LogEventListView, Path=SelectedItem, Converter={StaticResource NullToVisibilityConverter}}"&gt;
                  &lt;MenuItem.Icon&gt;
                      &lt;Image MaxHeight="16" MaxWidth="16" Source="{Binding ElementName=LogEventListView, Path=SelectedItem.Category, Converter={StaticResource LogEventCategoryConverter}, ConverterParameter='Small'}" /&gt;
                  &lt;/MenuItem.Icon&gt;
              &lt;/MenuItem&gt;
  </code></pre>
  
  <p>Everything works fine except for the binding of the first menu item.  When an item is not selected, I want the first menu item's Visibility to be Collapsed.  I also want the context MenuItem image to match that of the selected log event.   I have verified that BOTH of my IValueConverter classes are working properly.  For some reason the first MenuItem is always visible and never has an icon.  Can someone tell me what I'm overlooking?</p>
  
  <p><strong>UPDATE:</strong>
  There seem to be some real issues with binding to the Icon property of a MenuItem in .NET 3.5 as seen <a href="https://stackoverflow.com/questions/700681/wpf-binding-to-a-menu-icon">here</a> and <a href="http://connect.microsoft.com/VisualStudio/feedback/details/497408/wpf-menuitem-icon-cannot-be-set-via-setter#" rel="nofollow noreferrer">here</a>.  Problems are compounded by the fact that I'm using an IValueConverter to select the appropriate image.  Although it's <strong>not</strong> the solution I prefer, for now I've just decided to set the values in code-behind in the ContextMenu's opening event.</p>
  
  <pre><code>ContextMenu menu = sender as ContextMenu;
  
  if (menu != null)
  {
          MenuItem item = LogicalTreeHelper.FindLogicalNode(menu, "ContextMenuViewDetails") as MenuItem;
  
          if (item != null)
          {
              if (this.LogEventListView.SelectedItems.Count &lt;= 0)
                  item.Visibility = Visibility.Collapsed;
              else
                  item.Visibility = Visibility.Visible;
              }
          }
  }
  </code></pre>