CommandParameters in ContextMenu in WPF

后端 未结 4 1013
半阙折子戏
半阙折子戏 2020-12-01 13:19

I have a scenario where I have a WPF TreeView control that has an HierarchicalDataTemplate for its items. Now inside the HierarchicalDataTemplate,

相关标签:
4条回答
  • 2020-12-01 13:34
    <MenuItem Header="..." 
              Command="{Binding Path=...}" 
              CommandParameter="{Binding Path=PlacementTarget, RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType={x:Type ContextMenu}}}">
    </MenuItem>
    

    ContextMenu.PlacementTarget, is Label, where the menuitem is hosted. From Lavel, its parent Treeview is accessable.

    0 讨论(0)
  • 2020-12-01 13:39
    <ContextMenu>
        <MenuItem Header="Edit Item"
                       Command="{Binding EditItemCommand, Mode=OneWay}"
                       CommandParameter="{Binding Path=UIElement.(views:DataGridView.SelectedItems), RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}" />
    <ContextMenu>
    
    0 讨论(0)
  • 2020-12-01 13:46

    The problem is that the ContextMenu is at the root of its own visual tree, so any RelativeSource.FindAncestor bindings won't go past the ContextMenu.

    One solution is to use the PlacementTarget property to set up a two-stage binding from your Label:

    <Label Tag="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={
        x:Type TreeView}}}">
        <Label.ContextMenu>
            <ContextMenu>
                <MenuItem Header="Delete" Command="{x:Static local:Commands.DeleteCommand}"
                    CommandParameter="{Binding PlacementTarget.Tag, RelativeSource={
                    RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}"/>
            </ContextMenu>
        </Label.ContextMenu>
    </Label>
    

    This is quite hacky, however. You're better off setting the CommandTarget property of your MenuItem to the ContextMenu's PlacementTarget and having the command handler on your TreeView. This means you won't have to pass the TreeView around.

    0 讨论(0)
  • 2020-12-01 13:47

    Take a look at WPF CommandParameter Binding Problem. Maybe it can provide some pointers as to what's going on.

    0 讨论(0)
提交回复
热议问题