Command binding not working in a dynamic MVVM Context Menu

纵然是瞬间 提交于 2019-12-09 20:24:44

问题


I am new to WPF. Like many others, I am trying to bind a ContextMenu to an ObservableCollection to create a dynamic context menu. Everything works except binding the Command property to the TheCommand property of the MenuItemViewModel class, that represents the menu item. The command is not fired. What am I doing wrong?

To start from the beginning, the ContextMenu is a child of the Image and is shown when the mouse is over the Image.

 <Image.ContextMenu >
        <ContextMenu ItemsSource="{DynamicResource ContextMenu}"

where the empty ContextMenu is defined as follows:

<Window.Resources>
    <local:MenuItemViewModelCollection x:Key="ContextMenu">
    </local:MenuItemViewModelCollection>

    <HierarchicalDataTemplate DataType="{x:Type local:MenuItemViewModel}"
                                      ItemsSource="{Binding Path=Children}">
        <HierarchicalDataTemplate.ItemContainerStyle>
            <Style TargetType="MenuItem">
                <Setter Property="Command"
                    Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}},
                                    Path=DataContext.TheCommand}"/>
              <!--  Value="{Binding Path=TheCommand}" /> I tried this too -->

            </Style>
        </HierarchicalDataTemplate.ItemContainerStyle>
    </HierarchicalDataTemplate>
</Window.Resources>

The TheCommand property is defined below:

public class MenuItemViewModel : INotifyPropertyChanged
{
       //...
       public ICommand TheCommand
       {
             //...
       }
}

回答1:


DataContext on ContextMenus can be weird, I bet if you look in the output window in Visual Studio when debugging that there will be a binding error for TheCommand not being found. Try the following:

<Setter Property="Command" Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.DataContext.TheCommand}"/> 

This will use the DataContext of the element that the ContextMenu is launched from, not the context menu itself.




回答2:


Did you try

Value="{TemplateBinding TheCommand}" ?




回答3:


Look at my answer for the following question -

Context Menu items command binding WPF using MVVM

Hope it helps!



来源:https://stackoverflow.com/questions/8185467/command-binding-not-working-in-a-dynamic-mvvm-context-menu

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