[英]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.