将ContextMenu的MenuItem可见性绑定到ListView选择 [英] Bind ContextMenu's MenuItem visibility to ListView selection

查看:84
本文介绍了将ContextMenu的MenuItem可见性绑定到ListView选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有ListView的用户控件,其中包含来自ObservableCollection的简单项目.我希望该ListView的ContextMenu包含项目,具体取决于在ListView中选择的内容.如果未选择任何项目,则某些MenuItems应该不可见.

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.

当我打开ContextMenu时,甚至没有调用我的转换器.绑定似乎是错误的,我在输出窗口中发现了这一点:

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错误:4:找不到参考'ElementName = listView'的绑定源. BindingExpression:Path = SelectedItem; DataItem = null;目标元素是'MenuItem'(Name ='');目标属性为可见性"(类型为可见性")

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.

这是一些简化的代码:

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

非常感谢!

推荐答案

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

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}}" />

这篇关于将ContextMenu的MenuItem可见性绑定到ListView选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆