How to set ContextMenu of a bound item?

≡放荡痞女 提交于 2019-12-06 01:28:32

问题


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>

But it throws the following exception:

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

回答1:


Try this instead:

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

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


来源:https://stackoverflow.com/questions/1397497/how-to-set-contextmenu-of-a-bound-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!