您如何知道 ItemsControl 的哪个元素在 MVVM 中发送事件?
假设我当前有一个 ItemsControl,其 DataTemplate 是一堆按钮。我正在连接这些按钮的单击事件,但是我如何知道单击了哪个按钮?我不应该使用 ItemsControl 吗?
我试图不使用任何代码隐藏,但务实可能是必要的。
<ItemsControl>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Margin="10">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding ItemsControlButtonClicked, Mode=OneWay}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
Let's say I currently have an ItemsControl whose DataTemplate is a bunch of buttons. I'm wiring up these buttons' click events, but how am I to know which button was clicked? Should I not use a ItemsControl?
I'm trying to have no code-behind, but being pragmatic may be necessary.
<ItemsControl>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Margin="10">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding ItemsControlButtonClicked, Mode=OneWay}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您想知道单击了什么
Item
,请将{Binding }
作为CommandParameter
传递,它会将所选对象传递给您的 CommandIf如果您想知道单击了什么
Button
,我会在代码隐藏中执行此操作,因为 ViewModel 不需要了解有关 UI 的任何信息,其中包括按钮。此外,由于您的控件是按钮,因此您应该使用
Command
属性而不是 Click 触发器。If you want to know what
Item
was clicked, then pass{Binding }
as theCommandParameter
and it will pass the selected object to your CommandIf you want to know what
Button
was clicked, I would do that in the code-behind since ViewModels do not need to know anything about the UI, and that includes buttons.Also since your control is a Button, you should use the
Command
property instead of a Click trigger.您可以随命令一起发送参数,并根据这些参数您可以找出单击了哪个按钮
You can send parameters along with the command and based on these parameters you can find out which button was clicked
在我的项目中,我还使用 MVVM Light 我有一个包含项目集合的下拉菜单,以及一个用户按下的按钮和操作取决于从下拉菜单中选择的项目
您应该创建一个带有参数的 Relay 命令,请查看我的代码中的示例,
我将下拉列表与类 Project 的集合绑定在一起
对于按钮命令参数,我绑定所选项目表单下拉列表
查看代码,
注意 Command 和 CommandParameter 绑定
,您也可以使用此方法,不仅用于下拉菜单
In my project I also use the MVVM Light I has an dropdown with collection of items, and a button which user press and action depend on selected item from drop down
you should create a Relay command with parameter look at the example from my code
on the view I bind the drop down with collection of class Project
and for button command parameter I bind the selected item form drop down
look at the code
pay attention to Command and CommandParameter binding
also you can use this approache not only for drop down
那么,您可以使用 Sender.DataContext 这是实际的数据。
Well, you can use the Sender.DataContext which is the actual data.
在视图模型类中创建命令属性(使用 Josh Smith 的
RelayCommand
模式是执行此操作的最简单方法)并将每个按钮的Command
绑定到相应的按钮。这不仅简单易行且易于维护,而且还为您提供了一种在需要时实现启用/禁用行为的简单方法。Create command properties in your view model class (using Josh Smith's
RelayCommand
pattern is the simplest way to do this) and bind each button'sCommand
to the appropriate one. Not only is this straightforward to do and simple to maintain, it also gives you an easy way of implementing the enable/disable behavior when you need to.