将 ContextMenu 的 MenuItem 可见性绑定到 ListView 选择
我有一个带有 ListView 的用户控件,其中包含 ObservableCollection 中的简单项目。 我希望该 ListView 的 ContextMenu 包含根据 ListView 中选择的内容的项目。 如果未选择任何项目,则某些菜单项不应可见。
当我打开上下文菜单时,我的转换器甚至没有被调用。 绑定似乎是错误的,我在输出窗口中发现:
System.Windows.Data 错误:4:找不到引用“ElementName=listView”的绑定源。 BindingExpression:Path=SelectedItem; 数据项=空; 目标元素是“MenuItem”(名称=“”); 目标属性是“Visibility”(类型“Visibility”)
我不明白出了什么问题,也无法通过搜索网络找出答案。
这是一些简化的代码:
<UserControl x:Class="MyApp.DatabaseControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:l="clr-namespace:MyApp"
Height="Auto"
Width="Auto">
<UserControl.Resources>
<l:ValueToVisibilityConverter x:Key="valueToVisibility" />
</UserControl.Resources>
<Grid>
<ListView x:Name="listView" ItemsSource="{Binding Persons}">
<ListView.View>
<GridView>
<GridViewColumn Width="140" Header="First Name" DisplayMemberBinding="{Binding FirstName}"/>
<GridViewColumn Width="140" Header="Last Name" DisplayMemberBinding="{Binding LastName}" />
</GridView>
</ListView.View>
<ListView.ContextMenu>
<ContextMenu>
<MenuItem
Header="Open"
Visibility="{Binding SelectedItem, ElementName=listView, Converter={StaticResource valueToVisibility}}"/>
<Separator/>
<MenuItem Header="Add..."/>
<MenuItem Header="Remove"/>
</ContextMenu>
</ListView.ContextMenu>
</ListView>
</Grid>
多谢!
I have a user control with a ListView containing simple items from an ObservableCollection. I would like the ContextMenu of that ListView to contain items depending on what's selected in the ListView. If no item is selected, some MenuItems should not be visible.
My converter isn't even called when I open the ContextMenu. The binding seems to be wrong, I find this in the output window:
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=listView'. BindingExpression:Path=SelectedItem; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'Visibility' (type 'Visibility')
I don't understand what's wrong and could not figure it out by searching the web.
Here is some simplified code:
<UserControl x:Class="MyApp.DatabaseControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:l="clr-namespace:MyApp"
Height="Auto"
Width="Auto">
<UserControl.Resources>
<l:ValueToVisibilityConverter x:Key="valueToVisibility" />
</UserControl.Resources>
<Grid>
<ListView x:Name="listView" ItemsSource="{Binding Persons}">
<ListView.View>
<GridView>
<GridViewColumn Width="140" Header="First Name" DisplayMemberBinding="{Binding FirstName}"/>
<GridViewColumn Width="140" Header="Last Name" DisplayMemberBinding="{Binding LastName}" />
</GridView>
</ListView.View>
<ListView.ContextMenu>
<ContextMenu>
<MenuItem
Header="Open"
Visibility="{Binding SelectedItem, ElementName=listView, Converter={StaticResource valueToVisibility}}"/>
<Separator/>
<MenuItem Header="Add..."/>
<MenuItem Header="Remove"/>
</ContextMenu>
</ListView.ContextMenu>
</ListView>
</Grid>
Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是
ContextMenu
与ListBox
不在同一可视树中,因此绑定找不到ListBox
。 如果您绑定PlacementTarget
,应该可以解决问题:The problem is that the
ContextMenu
is not in the same visual tree as theListBox
, therefore bindings don't find theListBox
. If you bind againstPlacementTarget
, that should do the trick: