0

I try to use a context menu from a user control's list view but the command is not firing (neither enabling/disabling when needed).

Code:

<UserControl ....
    <UserControl.Resources>
        <ContextMenu x:Key="SharedInstanceContextMenu">
            <MenuItem Header="Edit" Command="{Binding ElementName=UC, Path=DataContext.EditSelectedItemCommand}" CommandParameter="{Binding}"/>
        </ContextMenu>

<Grid ...>
    <ListView ....>
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="IsSelected" Value="{Binding Path=IsSelected}"/>
                <Setter Property="ContextMenu" Value="{StaticResource SharedInstanceContextMenu}"/>
            </Style>
        </ListView.ItemContainerStyle>   

How can I make the command firing (and enabling/disabling, part of the command behavior)?

(btw, this questions seems similar to Treeview context menu command not firing but after trying all solutions there it still does not work.)

4

1 回答 1

1

您的输出窗口是否包含绑定错误,抱怨您的视图模型上不存在该命令?如果是这样,则可能意味着您的 ContextMenu 的 DataContext 设置不正确。上下文菜单不是可视化树的一部分,因为它们需要在元素顶部弹出,这意味着它们不会像其他控件那样继承其 DataContext。一种解决方案是使用 PlacementTarget 访问您的视图模型 - 请参阅这篇文章了解更多信息。

于 2012-04-19T01:11:49.690 回答