简体   繁体   中英

WPF Binding to ListView SelectedItem Is Not Working

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.

Here is how I'm populating the ListView:

// 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;

And here is where I'm creating my ListView control.

    <ListView x:Name="LogEventListView" 
          Grid.Row="0"
          Grid.Column="0"
          SelectionMode="Single"
          VirtualizingStackPanel.IsVirtualizing="True">
    <ListView.ContextMenu>
        <ContextMenu Opened="ContextMenu_Opened">
            <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}}">
                <MenuItem.Icon>
                    <Image MaxHeight="16" MaxWidth="16" Source="{Binding ElementName=LogEventListView, Path=SelectedItem.Category, Converter={StaticResource LogEventCategoryConverter}, ConverterParameter='Small'}" />
                </MenuItem.Icon>
            </MenuItem>

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?

UPDATE: There seem to be some real issues with binding to the Icon property of a MenuItem in .NET 3.5 as seen here and here . Problems are compounded by the fact that I'm using an IValueConverter to select the appropriate image. Although it's not the solution I prefer, for now I've just decided to set the values in code-behind in the ContextMenu's opening event.

ContextMenu menu = sender as ContextMenu;

if (menu != null)
{
        MenuItem item = LogicalTreeHelper.FindLogicalNode(menu, "ContextMenuViewDetails") as MenuItem;

        if (item != null)
        {
            if (this.LogEventListView.SelectedItems.Count <= 0)
                item.Visibility = Visibility.Collapsed;
            else
                item.Visibility = Visibility.Visible;
            }
        }
}
<Image MaxHeight="16" MaxWidth="16" 
Source="{Binding ElementName=LogEventListView, 
Path=SelectedItem.Category, 
Converter={StaticResource LogEventCategoryConverter}, ConverterParameter='Small'}"/>

I can't seem to find an attached property called Category to the SelectedItem ??

Edit: after look at it again, this might actually be an example of the problem described here

Original answer below, but probably won't work

Without seeing the converter I can't comment on why it might not be working, but you could try to achieve the same with a style instead:

<ContextMenu.Style>
    <Style>
       <Style.Triggers>
           <DataTrigger Binding="{Binding SelectedItem, ElementName=LogEventListView} Value="{x:Null}">
              <Setter Property="Visibility" Value="Collapsed"/>
           </DataTrigger>
       </Style.Triggers>
     </Style>
 </ContextMenu.Style>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM