不带标签的 DataTemplate 中 MenuItem 的绑定命令
我想将 ViewModel 中的命令绑定到 DataTemplate 中的 menuItem。我可以使用标签来做到这一点。有没有任何方法可以完成相同的任务但不使用标签。
<Window.Resources>
<DataTemplate x:Key="StudentListBoxItemTemplate">
<StackPanel Tag="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}">
<TextBlock Text="{Binding Name}"/>
<StackPanel.ContextMenu>
<ContextMenu>
<MenuItem Header="Trigger" Command="{Binding PlacementTarget.Tag.TriggerCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
</ContextMenu>
</StackPanel.ContextMenu>
</StackPanel>
</DataTemplate>
</Window.Resources>
<StackPanel>
<ListBox
ItemsSource="{Binding StudentList}"
ItemTemplate="{StaticResource StudentListBoxItemTemplate}">
</ListBox>
</StackPanel>
我的视图模型
public class MainViewModel {
public ICommand TriggerCommand { ... }
public ObservableList<Student> StudentList { ... }
}
I want to bind a command in my ViewModel to a menuItem which is in DataTemplate. I can do that with using Tag. Is there any method which can do the same task but without using tag.
<Window.Resources>
<DataTemplate x:Key="StudentListBoxItemTemplate">
<StackPanel Tag="{Binding DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}">
<TextBlock Text="{Binding Name}"/>
<StackPanel.ContextMenu>
<ContextMenu>
<MenuItem Header="Trigger" Command="{Binding PlacementTarget.Tag.TriggerCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
</ContextMenu>
</StackPanel.ContextMenu>
</StackPanel>
</DataTemplate>
</Window.Resources>
<StackPanel>
<ListBox
ItemsSource="{Binding StudentList}"
ItemTemplate="{StaticResource StudentListBoxItemTemplate}">
</ListBox>
</StackPanel>
My ViewModel
public class MainViewModel {
public ICommand TriggerCommand { ... }
public ObservableList<Student> StudentList { ... }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据您当前的设计,您需要从
ContextMenu
通过StackPanel
并返回到包含ListBox
的DataContext
代码>.造成这种尴尬的原因是StackPanel
的DataContext
已经缩小到特定的学生。至少有两种方法可以让这变得更容易:
Student
中提供TriggerCommand
属性,以便该命令就在您需要的地方Parent
Student
中的 > 属性来逃避缩小的范围With your current design, you need to get from the
ContextMenu
through theStackPanel
and back to theDataContext
of the containingListBox
. What makes this awkward is that theDataContext
of theStackPanel
is already narrowed down to a particular student.There are at least two ways to make this easier:
TriggerCommand
property inStudent
so the command is right there where you need itParent
property in theStudent
to escape the narrowed scope您可以尝试将点击事件添加到menuItem,如下
尝试将命令绑定到Click。我的 VS 已关闭,因此目前无法检查。
You can try to add click event to the menuItem like following
Try binding the command to Click. My VS is down so can't check at this moment.
一种方法是获取视图模型中定义的上下文菜单集合演示,其中将包含标题字符串和命令操作(可能带有谓词)。
视图模型创建上下文菜单项的可观察集合,视图将其绑定到上下文菜单项源并将显示成员路径设置为标题字符串。
One way would be to get the context menu collection presentation defined in your viewmodel, which will contain the header string and command action (maybe with predicate).
The viewmodel creates an observable collection of the contextmenu items and the view binds that to ContextMenu itemssource and sets the displaymember path to header string.