简体   繁体   English

在按钮中单击上下文菜单如何绑定到viewModel?

[英]In Button Click Context Menu How do I bind to viewModel?

I have a button on click on that button opening a context menu, now clicking on the context menus is to be binded to viewModel. 我有一个按钮,单击该按钮会打开一个上下文菜单,现在单击上下文菜单将绑定到viewModel。 But its not happening. 但是它没有发生。

<Button Content="Copy" Tag="{Binding LinkViewModel, RelativeSource={RelativeSource Mode=Self}}" Command="{Binding LinkCopyCommand, UpdateSourceTrigger=PropertyChanged}" >
    <Button.ContextMenu>
        <ContextMenu>
           <MenuItem Header="Copy Download link " Command="{Binding Path=Parent.PlacementTarget.Tag.CopyViewCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}" />
           <MenuItem ... />
        </ContextMenu>
    </Button.ContextMenu> 
</Button>

I have tried the tag property but it seems to me that its not working. 我已经尝试了标签属性,但在我看来它不起作用。 The viewmodel is working fine if I bind to the button itself, but the contextMenu dataBinding is not working. 如果绑定到按钮本身,则viewmodel可以正常工作,但是contextMenu dataBinding无法正常工作。

EDIT: 编辑:

Now as the code is working after discussion, I think to post it here. 现在,由于代码在讨论之后正在工作,我想将其发布在这里。

What the changes I made is I put UpdateSourceTrigger="Propertychanged" here is the code 我所做的更改是,我将UpdateSourceTrigger =“ Propertychanged”放在此处,将代码

<Button Content="Copy" Tag="{Binding LinkViewModel, RelativeSource={RelativeSource Mode=Self}}" Command="{Binding LinkCopyCommand, UpdateSourceTrigger=PropertyChanged}" >
    <Button.ContextMenu>
       <ContextMenu Width="{Binding RelativeSource={RelativeSource Self}}">
          <MenuItem Header="Copy View link " Command="{Binding CopyViewCommand, UpdateSourceTrigger=PropertyChanged}" />
          <MenuItem ... />
       </ContextMenu>
    </Button.ContextMenu> 
</Button>

However I don't know how come suddenly it works, it has to work with tag property in case of Button Context menu. 但是我不知道它怎么突然起作用,在Button Context菜单的情况下它必须与tag属性一起工作。 If anybody put some light into this I think many people like me who are new WPF and data binding will be benefited. 如果有人对此有所了解,我认为许多像我这样的人都是新的WPF和数据绑定的受益者。

I'm assuming you are using this Button inside the UserControl . 我假设您正在UserControl中使用此Button。 Please try below code 请尝试以下代码

<Button Content="Copy" Tag="{Binding LinkViewModel, RelativeSource={RelativeSource Mode=Self}}" Command="{Binding LinkCopyCommand, UpdateSourceTrigger=PropertyChanged}" >
        <Button.ContextMenu>
            <ContextMenu>
               <MenuItem Header="Copy Download link " Command="{Binding RelativeSource={RelativeSource FindAncestor,  AncestorType={x:Type UserControl}}, Path=DataContext.CopyViewCommand}" />
               <MenuItem ... />
            </ContextMenu>
        </Button.ContextMenu> 
    </Button>
<Button Content="Copy" Command="{Binding LinkCopyCommand, UpdateSourceTrigger=PropertyChanged}" >
<Button.ContextMenu>
    <ContextMenu>
       <MenuItem Header="Copy Download link " Command="{Binding Path=CopyViewCommand}" />
       <MenuItem ... />
    </ContextMenu>
</Button.ContextMenu> 

CopyViewCommand Bound directly from your DataContext ... which is your ViewModel.. 直接从您的DataContext ...(即ViewModel)绑定CopyViewCommand

You have to setyour ContextMenu's DataContext to your ViewModel. 您必须将ContextMenu的DataContext设置为ViewModel。 One way to do this is by having an Opened eventhandler for the context menu. 一种方法是为上下文菜单设置一个Opened事件处理程序。

Take a look at my answer in the below link - 在下面的链接中查看我的答案-

Context Menu items command binding WPF using MVVM 上下文菜单项命令使用MVVM绑定WPF

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

相关问题 如何将按钮的Hold / DoubleTap事件绑定到ViewModel中的属性 - How can I bind a Hold/DoubleTap event of a button to a property in ViewModel 如何将listviewitem中的按钮绑定到Winrt中的ViewModel中的Command - How can i bind a button in listviewitem to a Command in ViewModel in Winrt WPF上下文菜单不会绑定到VIewModel属性 - WPF Context Menu won't Bind To VIewModel property 如何绑定到不是当前绑定上下文的viewmodel上的属性 - How to bind to property on viewmodel that is not the current binding context 如何将图标绑定到资源中定义的上下文菜单 - How do you bind Icons to context menu defined in the resources 如何在外部定义的上下文菜单中绑定子菜单的复选标记? - How do you bind the checkmarks of submenus in a context menu defined externally? 当我单击鼠标左键时如何显示上下文菜单 c# - how to show context menu when i click on left mouse button c# 右键单击 DataGridView 的列标题时,如何正确定位上下文菜单? - How do I correctly position a Context Menu when I right click a DataGridView's column header? MVVM-WPF如何将我的视图绑定到我的Viewmodel? - MVVM - WPF How do i bind my View to my Viewmodel? 如何从 DataTemplate 内部绑定到 ViewModel 上的属性? - How do I bind to a property on the ViewModel from inside a DataTemplate?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM