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

查看:967
本文介绍了如何处理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天全站免登陆