在 SelectionChanged 事件上使用交互触发器的正确方法
我有一个命令连接到该事件,以便它确实触发,但我在 CommandParameter 中得到的是先前选定的项目,或者可能是 SelectionChanged 完成之前选定的项目。
无论哪种方式,都不知道要更改什么才能从事件中获取新选择的项目。
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<cmd:EventToCommand
Command="{Binding Main.SelectedRecordCommand, Source={StaticResource Locator}}"
CommandParameter="{Binding SelectedItem, ElementName=listBillingRecords}"
/>
</i:EventTrigger>
</i:Interaction.Triggers>
谢谢
I have a command wired to the event such that it does fire, but what I get in the CommandParameter is the previously selected item, or maybe it's the selected item before the SelectionChanged completes.
Either way, not sure what to change to get the newly selected item from the event.
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<cmd:EventToCommand
Command="{Binding Main.SelectedRecordCommand, Source={StaticResource Locator}}"
CommandParameter="{Binding SelectedItem, ElementName=listBillingRecords}"
/>
</i:EventTrigger>
</i:Interaction.Triggers>
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
值得使用触发器吗?如果集合的 XAML 元素(列表框、网格等)绑定到在视图模型上公开集合的属性,则可以利用数据绑定和内置 MVVM Light 信使来通知您属性更改以更 MVVM 友好的方式处理新旧值。这个例子不一定是WP7特定的,但我认为它的工作原理是一样的。
例如,这可能是数据绑定集合:
我喜欢在 ViewModel 上公开一个属性,该属性是我公开的任何集合的“选定项”。因此,对于 ViewModel,我将使用 MVVMINPC 代码段添加此属性:
现在,如果将 XAML 元素的 SelectedItem 绑定到此公开属性,则当通过数据绑定在视图中选择时,它将填充该属性。
但更好的是,当您利用片段 MVVMINPC 时,您可以选择是否向任何收听的人广播结果。在本例中,我们想知道 SelectedBillingRecord 属性何时发生变化。因此,您可以在 ViewModel 的构造函数中包含此内容:
在 ViewModel 的其他位置,无论您希望发生什么操作:
希望这会有所帮助...
Is it worth using a trigger? If whatever your XAML element is for the collection (listbox, grid, etc) is bound to a property exposing a collection on your viewmodel, you can leverage both databinding and the built-in MVVM Light messenger to notify you of a property change with both old and new values in a more MVVM-friendly way. This example isn't necessarily WP7-specific, but I think it would work the same.
For example, this might be the databound collection:
I like to expose a property on my ViewModel that is a "selected item" of whatever collection I'm exposing. So, to the ViewModel, I would add this property using the MVVMINPC snippet:
Now, if you bind the SelectedItem of the XAML element to this exposed property, it will populate when selected in the View via databinding.
But even better, when you leverage the snippet MVVMINPC, you get to choose whether or not to broadcast the results to anyone listening. In this case, we want to know when the SelectedBillingRecord property changes. So, you can have this in the constructor for your ViewModel:
And elsewhere in your ViewModel, whatever action you want to have happen:
Hope this helps...
我看到了同样的问题,并发现 SelectedItem 是正确的实现。
I have seen the same issue and found that SelectedItem is the correct implementation.