简体   繁体   English

如何将命令绑定到 DataTemplate 中的 ContextMenu

[英]How to bind command into ContextMenu in DataTemplate

I'm a little bit lost with bindings.我对绑定有点迷茫。 I tried so many things in the last hour, I cannot enumerate all of them.在过去的一个小时里我尝试了很多东西,我无法一一列举。 I have an issue with a contextMenu inside a DataTemplate.我在 DataTemplate 中的 contextMenu 有问题。

To explain: I have a UserControl .解释一下:我有一个UserControl Its dataContext is itself.它的 dataContext 是它本身。 Inside this UserControl , I have an ItemsControl to represent a list of Hyperlink.在这个UserControl ,我有一个 ItemsControl 来表示超链接列表。 My ItemsControl itemsSource is bound (it is composed of objects elements).我的ItemsControl itemsSource已绑定(它由对象元素组成)。 I redefined ItemsControl.ItemTemplate .我重新定义了ItemsControl.ItemTemplate Inside, I create a HyperLink, with TextBlock as child to make it work, and on this TextBlock , I set a ContextMenu by doing the following.在里面,我创建了一个 HyperLink,以TextBlock作为子项以使其工作,在这个TextBlock ,我通过执行以下操作设置了一个ContextMenu

<TextBlock.ContextMenu>
  <ContextMenu DataContext="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}">
    <MenuItem Header="Enregistrer la pièce jointe" Foreground="Black">
      <MenuItem Header="Dans le dossier patient" Command="{Binding DataContext.SaveAttachmentIntPatientFolderCommand, RelativeSource={RelativeSource AncestorType=UserControl}}" CommandParameter="{Binding FilePath}" Foreground="Black" />
      <MenuItem Header="Enregistrer sous ..." Command="{Binding DataContext.SaveAttachmentAsCommand}" CommandParameter="{Binding FilePath}" Foreground="Black" />
    </MenuItem>
  </ContextMenu>
</TextBlock.ContextMenu>

So I have所以我有

UserControl --> ItemsControl --> ItemTemplate --> HyperLink --> TextBlock --> ContextMenu --> ContextMenuItem

I know that my first relative source doesn't work, I have a binding error.我知道我的第一个相对来源不起作用,我有一个绑定错误。 What I want is to bind on my UserContorl datacontext, which have these commands.我想要的是绑定我的 UserContorl 数据上下文,它有这些命令。

How can I proceed?我该如何继续?

Thanks谢谢

ContextMenu takes the DataContext of the ItemsControl and so it cannot access the ViewModel directly. ContextMenu 接受 ItemsControl 的 DataContext,因此它不能直接访问 ViewModel。 Also It is not part of the VisualTree and so you cannot do RelativeSource binding.此外,它不是 VisualTree 的一部分,因此您不能进行 RelativeSource 绑定。 So We need to get the DataContext of the UserControl through TextBlock's Tag property and then bind to ContextMenu.所以我们需要通过TextBlock的Tag属性来获取UserControl的DataContext,然后绑定到ContextMenu上。 You refer the below code.你参考下面的代码。

<TextBlock Text="{Binding }" Tag="{Binding DataContext, RelativeSource={RelativeSource AncestorType=UserControl}}">
  <TextBlock.ContextMenu>
    <ContextMenu >
      <MenuItem Header="Enregistrer la pièce jointe" Foreground="Black">
        <MenuItem Header="Dans le dossier patient" 
                  Command="{Binding Path=PlacementTarget.Tag.SaveAttachmentIntPatientFolderCommand,
                            RelativeSource={RelativeSource AncestorType=ContextMenu}}"                                                  
                  Foreground="Black" />
        <MenuItem Header="Enregistrer sous ..." 
                  Command="{Binding Path=PlacementTarget.Tag.SaveAttachmentAsCommand,
                            RelativeSource={RelativeSource AncestorType=ContextMenu}}"  
                  Foreground="Black" />
      </MenuItem>
    </ContextMenu>
  </TextBlock.ContextMenu>
</TextBlock>     

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 ItemTemplate 中将命令绑定到 ContextMenu? - How to bind a Command to a ContextMenu from within an ItemTemplate? 从DataTemplate和ItemsControl中的ContextMenu绑定CommandParameter - Bind CommandParameter from ContextMenu in DataTemplate and ItemsControl 将命令绑定到 ContextMenu 项 - Bind Command To ContextMenu Item 使用HierarchicalDataTemplate将命令绑定到ContextMenu项 - Bind Command to ContextMenu items with HierarchicalDataTemplate 如何将我的 ContextMenu 命令正确绑定到 RelayCommand? - How do I properly bind my ContextMenu command to a RelayCommand? 如何将 ViewModel 中的命令绑定到不属于自己的文件中的 DataTemplate - How to bind command from ViewModel to a DataTemplate which is in in't own file 如何将图像按钮命令从DataTemplate选择器绑定到ViewModel? - How to bind Image Button command from a DataTemplate Selector to a ViewModel? WinUI 3 使用 DataTemplate 时如何将命令绑定到 ViewModel 属性? - WinUI 3 How to Bind Command to ViewModel property when using a DataTemplate? 如何在WP7中的数据模板中更改上下文菜单的属性? - How to change the properties of a contextmenu in a datatemplate in WP7? 无法将ContextMenu操作绑定到Command - Can't bind a ContextMenu action to a Command
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM