简体   繁体   English

WPF绑定到ListView SelectedItem无效

[英]WPF Binding to ListView SelectedItem Is Not Working

I'm using a ListView to display contents of a log in my application. 我正在使用ListView在我的应用程序中显示日志的内容。 I want to change the icon and visibility of a context MenuItem based upon the user's currently selected entry in the ListView. 我想根据用户当前在ListView中选择的条目来更改上下文MenuItem的图标和可见性。

Here is how I'm populating the ListView: 这是我填充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控件的地方。

    <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. 我还希望上下文MenuItem图像与所选日志事件的图像匹配。 I have verified that BOTH of my IValueConverter classes are working properly. 我已经验证我的IValueConverter类都正常工作。 For some reason the first MenuItem is always visible and never has an icon. 出于某种原因,第一个MenuItem始终可见,并且永远没有图标。 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 . 更新:绑定到.NET 3.5中MenuItem的Icon属性似乎存在一些实际问题,如此此处所示 Problems are compounded by the fact that I'm using an IValueConverter to select the appropriate image. 我正在使用IValueConverter选择适当的图像,这使问题更加复杂。 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的open事件中的代码后面设置值。

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 ?? 我似乎找不到一个名为Category的附加属性到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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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