【问题标题】:WPF Binding to ListView SelectedItem Is Not WorkingWPF 绑定到 ListView SelectedItem 不起作用
【发布时间】:2011-08-05 21:09:27
【问题描述】:

我正在使用 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 属性似乎存在一些实际问题,如 herehere 所示。由于我使用 IValueConverter 来选择适当的图像,问题变得更加复杂。虽然它不是我更喜欢的解决方案,但现在我决定在 ContextMenu 的打开事件中设置代码隐藏值。

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

【问题讨论】:

  • 请发布您的转换器。你已经验证了转换器被调用了吗?
  • 我已验证转换器工作正常。我在其他地方使用过它们。由于某种原因,他们没有被这个控件调用。

标签: c# wpf xaml data-binding


【解决方案1】:

编辑:再看一遍,这可能实际上是here所描述的问题的一个例子@

以下原始答案,但可能行不通

没有看到转换器,我无法评论为什么它可能不起作用,但您可以尝试使用样式来实现相同的效果:

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

【讨论】:

  • 感谢您的建议。不幸的是,我也无法让这些样式起作用。绑定 MenuItem 属性似乎存在真正的问题,尤其是 Icon 属性。
【解决方案2】:
<Image MaxHeight="16" MaxWidth="16" 
Source="{Binding ElementName=LogEventListView, 
Path=SelectedItem.Category, 
Converter={StaticResource LogEventCategoryConverter}, ConverterParameter='Small'}"/>

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

【讨论】:

  • Category 是绑定到 ListView 的 ObservableCollection 中项目的属性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-20
  • 1970-01-01
  • 2018-07-07
  • 2023-03-25
  • 1970-01-01
  • 2011-08-19
  • 1970-01-01
相关资源
最近更新 更多