如何设置绑定项的ContextMenu? [英] How to set ContextMenu of a bound item?

查看:152
本文介绍了如何设置绑定项的ContextMenu?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现以下目标:

I am trying to achieve the following:

<Style TargetType="ListBoxItem">
    <Setter Property="ContextMenu">
        <Setter.Value>
            <ContextMenu>
                <MenuItem Name="mnuEdit" Header="_Edit" Click="MenuItem_Click" />
            </ContextMenu>
        </Setter.Value>
    </Setter>
<Style>

但是会引发以下异常:

Cannot add content of type 'System.Windows.Controls.ContextMenu'
to an object of type 'System.Object'.
Error at object 'System.Windows.Controls.ContextMenu'
in markup file blah blah blah


推荐答案

试试看:

<ContextMenu x:Key="contextMenu">
    <MenuItem Name="mnuEdit" Header="_Edit" Click="MenuItem_Click" />
</ContextMenu>

<Style TargetType="ListBoxItem">
    <Setter Property="ContextMenu" Value="{DynamicResource contextMenu}" />
</Style>

这篇关于如何设置绑定项的ContextMenu?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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