在 DataTemplate 中绑定 ContextMenu
我有:
<ListBox>
<ListBox.Resources>
<DataTemplate DataType="{x:Type ViewModels:StyleViewModel}">
<DockPanel>
<Button Content="{Binding Name}" Tag="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}">
<Button.ContextMenu>
<ContextMenu>
<MenuItem Header="Delete" Command="{Binding PlacementTarget.Tag.DataContext.RemoveMember1FavoriteStyleCommand}" CommandParameter="{Binding}" />
</ContextMenu>
</Button.ContextMenu>
</Button>
</DockPanel>
</DataTemplate>
</ListBox.Resources>
</ListBox>
我想要实现的是将上下文菜单的菜单项中的命令绑定到在视图模型中定义的 ICommand,该视图模型是列表框的数据上下文,并且命令参数应该是 StyleViewModel,但是我尝试过没有成功。有人能指出我正确的方向吗?
I have:
<ListBox>
<ListBox.Resources>
<DataTemplate DataType="{x:Type ViewModels:StyleViewModel}">
<DockPanel>
<Button Content="{Binding Name}" Tag="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}">
<Button.ContextMenu>
<ContextMenu>
<MenuItem Header="Delete" Command="{Binding PlacementTarget.Tag.DataContext.RemoveMember1FavoriteStyleCommand}" CommandParameter="{Binding}" />
</ContextMenu>
</Button.ContextMenu>
</Button>
</DockPanel>
</DataTemplate>
</ListBox.Resources>
</ListBox>
What I'm trying to achieve is to bind the command in the menuitem of the context menu to an ICommand that is defined in a viewmodel that is the datacontext of the listbox, and the commandparameter should be the StyleViewModel, but what I tried didn't work. Can anyone point me in the right direction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
找到了!
Found it!
现在几乎可以工作,除了现在 CommandParameter="{Binding}" 没有返回 StyleViewModel:
我想知道是否可以完成......
Almost working now, except that now CommandParameter="{Binding}" is not returning the StyleViewModel:
I'm wondering if it can be done...