将 ContextMenu 的 MenuItem 可见性绑定到 ListView 选择

发布于 2024-07-24 08:29:01 字数 1812 浏览 3 评论 0原文

我有一个带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼中杀气 2024-07-31 08:29:01

问题是 ContextMenuListBox 不在同一可视树中,因此绑定找不到 ListBox。 如果您绑定 PlacementTarget,应该可以解决问题:

<MenuItem Header="Open"
    Visibility="{Binding RelativeSource={RelativeSource FindAncestor,
        AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItem,
        Converter={StaticResource valueToVisibility}}" />

The problem is that the ContextMenu is not in the same visual tree as the ListBox, therefore bindings don't find the ListBox. If you bind against PlacementTarget, that should do the trick:

<MenuItem Header="Open"
    Visibility="{Binding RelativeSource={RelativeSource FindAncestor,
        AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItem,
        Converter={StaticResource valueToVisibility}}" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文