C# 与ListView SelectedItem的WPF绑定不起作用
我正在使用ListView在我的应用程序中显示日志的内容。我想根据用户在ListView中当前选择的条目更改上下文菜单项的图标和可见性 下面是我填充ListView的方式:C# 与ListView SelectedItem的WPF绑定不起作用,c#,wpf,xaml,data-binding,C#,Wpf,Xaml,Data Binding,我正在使用ListView在我的应用程序中显示日志的内容。我想根据用户在ListView中当前选择的条目更改上下文菜单项的图标和可见性 下面是我填充ListView的方式: // Create the collection view source. m_CollectionViewSource = new CollectionViewSource(); m_CollectionViewSource.SortDescriptions.Add(new System.C
// 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>
除了第一个菜单项的绑定外,其他一切都正常。如果未选择某个项目,我希望第一个菜单项的可见性折叠。我还希望上下文菜单项图像与所选日志事件的图像匹配。我已经验证了我的两个IValueConverter类都工作正常。由于某些原因,第一个菜单项始终可见,并且没有图标。有人能告诉我我忽略了什么吗
更新:
在.NET3.5中,绑定到MenuItem的Icon属性似乎存在一些实际问题,如图和所示。我使用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;
}
}
}
ContextMenu=sender作为ContextMenu;
如果(菜单!=null)
{
MenuItem=LogicalTreeHelper.FindLogicNode(菜单,“ContextMenuViewDetails”)作为MenuItem;
如果(项!=null)
{
如果(this.LogEventListView.SelectedItems.Count
我似乎找不到名为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>
请发布您的转换器。您是否已验证转换器是否已调用?我已验证转换器是否正常工作。我已在其他位置使用过它们。由于某些原因,无法从此控件调用它们。类别是绑定到ListView的ObservableCollection中的项的属性。谢谢您的建议。抱歉当然,我也无法让样式正常工作。绑定菜单项属性,尤其是图标属性似乎存在真正的问题。
<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>