wpf 你如何处理 MVVM 中的 ComboBox SelectionChanged?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/950501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:31:22  来源:igfitidea点击:

How do you handle a ComboBox SelectionChanged in MVVM?

wpfeventsmvvmattachedbehaviors

提问by Edward Tanguay

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

对于那些做纯 MVVM 的人,你如何处理 ComboBox SelectionChanged 事件而不恢复到代码背后?

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

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

<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>

采纳答案by Peter

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

您将使用数据触发器在不同的 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.

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

回答by fabien

This post is quite old, but since I got the same issue. Here is how I solved it (using framework 4.0) : the idea is to use System.Windows.Interactivity.

这篇文章很旧,但因为我遇到了同样的问题。这是我解决它的方法(使用框架 4.0):想法是使用 System.Windows.Interactivity。

In the XAML :

在 XAML 中:

<ComboBox ItemsSource="{Binding Items}">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
            <i:InvokeCommandAction Command="{Binding SelectionChangedCommand}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ComboBox>

Then you just need to implement the SelectionChangedCommand in your viewmodel.

然后你只需要在你的视图模型中实现 SelectionChangedCommand 。

回答by HAdes

I'm not sure if what you're after is possible, but the way I do it is to simply bind the SelectedItem to a property on view model. Then within the property setter, I call any custom code that I want to happen i.e. setting other properties based on rule. If I need the selected item to be bound to an object aswell (for other bound controls to update) I set this in the setter too and send out a notification.

我不确定您所追求的是否可行,但我这样做的方法是将 SelectedItem 简单地绑定到视图模型上的属性。然后在属性设置器中,我调用我想要发生的任何自定义代码,即根据规则设置其他属性。如果我还需要将所选项目绑定到一个对象(用于更新其他绑定控件),我也在 setter 中设置它并发出通知。