【发布时间】:2014-02-06 06:22:59
【问题描述】:
我有一个单击该按钮打开上下文菜单的按钮,现在单击上下文菜单将绑定到 viewModel。但它没有发生。
<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>
我已经尝试过 tag 属性,但在我看来它不起作用。如果我绑定到按钮本身,视图模型工作正常,但 contextMenu 数据绑定不起作用。
编辑:
现在代码经过讨论可以正常工作,我想把它贴在这里。
我所做的更改是我把 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>
但是我不知道它是怎么突然起作用的,在按钮上下文菜单的情况下它必须与标签属性一起使用。如果有人对此有所了解,我想很多像我这样的新 WPF 和数据绑定的人都会受益。
【问题讨论】:
标签: c# wpf data-binding mvvm contextmenu