你如何处理 MVVM 中的 ComboBox SelectionChanged? [英] How do you handle a ComboBox SelectionChanged in MVVM?

查看:35
本文介绍了你如何处理 MVVM 中的 ComboBox SelectionChanged?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于那些使用纯 MVVM 的人,您如何处理 ComboBox SelectionChanged 事件而不返回到隐藏代码?

For those doing pure MVVM, how do you handle a ComboBox SelectionChanged event without reverting to code behind?

我试过例如AttachedBehaviors 但不支持 Event="SelectedChanged":

I tried e.g. AttachedBehaviors but Event="SelectedChanged" is not supported:

<ComboBox>
    <ComboBoxItem Content="Test1">
        <c:CommandBehaviorCollection.Behaviors>
            <c:BehaviorBinding Event="SelectionChanged" 
                               Command="{Binding SelectedChanged}"
                               CommandParameter="MainBorder123"/>
        </c:CommandBehaviorCollection.Behaviors>
    </ComboBoxItem>
    <ComboBoxItem Content="Test2"/>
    <ComboBoxItem Content="Test3"/>
</ComboBox>

推荐答案

您将使用数据触发器在不同的 UI 元素上触发事件,例如启用/禁用或可见/不可见"

You would use a data trigger to trigger an event on a different UI element such as "enable / disable, or visible /invisible"

如果您希望所选元素在其他 UI 元素中显示对象数据,则可以使用数据绑定并将 UI 数据显示元素的数据上下文设置为绑定到组合框中当前选定的项目.

If you want the selected element to show the object data in other UI elements then you would use data binding and set the datacontext of the UI data display elements to be bound to the currently selected item in the combo box.

这篇关于你如何处理 MVVM 中的 ComboBox SelectionChanged?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆