繁体   English   中英

WPF绑定到ListView SelectedItem无效

[英]WPF Binding to ListView SelectedItem Is Not Working

我正在使用ListView在我的应用程序中显示日志的内容。 我想根据用户当前在ListView中选择的条目来更改上下文MenuItem的图标和可见性。

这是我填充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;

这是我创建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>

除了绑定第一个菜单项之外,其他所有东西都工作正常。 当未选择某个项目时,我希望第一个菜单项的“可见性”被折叠。 我还希望上下文MenuItem图像与所选日志事件的图像匹配。 我已经验证我的IValueConverter类都正常工作。 出于某种原因,第一个MenuItem始终可见,并且永远没有图标。 有人可以告诉我我忽略了什么吗?

更新:绑定到.NET 3.5中MenuItem的Icon属性似乎存在一些实际问题,如此此处所示 我正在使用IValueConverter选择适当的图像,这使问题更加复杂。 尽管这不是我喜欢的解决方案,但到目前为止,我只是决定在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'}"/>

我似乎找不到一个名为Category的附加属性到SelectedItem

编辑:再次查看之后,这实际上可能是此处描述的问题的一个示例

以下是原始答案,但可能无法正常工作

在没有看到转换器的情况下,我无法评论为什么它可能无法正常工作,但是您可以尝试使用一种样式来实现相同的目的:

<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