如何正确绑定菜单项? [英] How to correctly bind menu items?
本文介绍了如何正确绑定菜单项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何正确地绑定动态创建的菜单项列表。我尝试了几件事,但似乎没有任何工作。我得到了正确的名称列表,但是我的ViewSwitchCommand似乎没有正确地触发。
How do i correctly bind a dynamical created list of menu items. I have tried several thing but none seem to work. I get the proper list of names, however my ViewSwitchCommand does not seem to fire correctly.
<MenuItem Foreground="White" Header="Names" ItemsSource="{Binding Player.ToonNames}" Command="{Binding ViewSwitchCommand}" CommandParameter="{Binding Header}"/>
但是,如果我不动态做它,这样做,所有的工作都可以得到它工作
However if i don't do it dynamically and do it like this then everything works just fine can get it to work
<MenuItem Foreground="White" Header="Names">
<MenuItem Foreground="Black" Header="Chat" Command="{Binding ViewSwitchCommand}" CommandParameter="player1" />
<MenuItem Foreground="Black" Header="Craft" Command="{Binding ViewSwitchCommand}" CommandParameter="player2" />
</MenuItem>
命令参数期望一个字符串..不知道是否是...希望这是一些东西简单的我只是忽略
The command parameter expects a string.. not sure if that is it... hopefully this is something simple i'm just overlooking
推荐答案
此代码适用于我:
<MenuItem Header="Names" ItemsSource="{Binding Player.ToonNames}">
<MenuItem.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter Property="Command" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=MenuItem}, Path=DataContext.ViewSwitchCommand}" />
<Setter Property="CommandParameter" Value="{Binding}" />
</Style>
</MenuItem.ItemContainerStyle>
</MenuItem>
这篇关于如何正确绑定菜单项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文