WPF 将 UI 事件绑定到 ViewModel 中的命令 [英] WPF Binding UI events to commands in ViewModel

查看:43
本文介绍了WPF 将 UI 事件绑定到 ViewModel 中的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在重构一个简单的应用程序以遵循 MVVM,我的问题是如何将 SelectionChanged 事件从我的代码中移到后面的 viewModel 中?我看过一些将元素绑定到命令的例子,但并没有完全掌握它.任何人都可以提供帮助.谢谢!

任何人都可以使用下面的代码提供解决方案吗?非常感谢!

公共部分类 MyAppView : Window{公共 MyAppView(){初始化组件();this.DataContext = new MyAppViewModel();//在此点下方插入创建对象所需的代码.}private void contactsList_SelectionChanged(对象发送者,System.Windows.Controls.SelectionChangedEventArgs e){//TODO: 在此处添加事件处理程序实现.//为每个选定的联系人获取标签并放入集合ObservableCollectioncontactListLabels = new ObservableCollection();foreach (ContactListModel contactList in contactsList.SelectedItems){foreach(contactList.AggLabels 中的 AggregatedLabelModel aggLabel){contactListLabels.Add(aggLabel);}}//按名称聚合contactListLabelsListCollectionView selectedLabelsView = new ListCollectionView(contactListLabels);selectedLabelsView.GroupDescriptions.Add(new PropertyGroupDescription("Name"));tagsList.ItemsSource = selectedLabelsView.Groups;}}

解决方案

您应该将 EventTrigger 与 Windows.Interactivity 命名空间中的 InvokeCommandAction 结合使用.下面是一个例子:

<i:Interaction.Triggers><i:EventTrigger EventName="SelectionChanged"><i:InvokeCommandAction Command="{Binding SelectedItemChangedCommand}"/></i:EventTrigger></i:Interaction.Triggers></列表框>

您可以通过转到添加引用>来引用System.Windows.Interactivity组件 >扩展.

完整的 i 命名空间是:xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity".>

I’m doing some refactoring of a simple application to follow MVVM and my question is how do I move a SelectionChanged event out of my code behind to the viewModel? I’ve looked at some examples of binding elements to commands but didn’t quite grasp it. Can anyone assist with this. Thanks!

Can anyone provide a solution using the code below? Many thanks!

public partial class MyAppView : Window 
{
    public MyAppView()
    {
        InitializeComponent();

        this.DataContext = new MyAppViewModel ();

        // Insert code required on object creation below this point.
    }

    private void contactsList_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
        //TODO: Add event handler implementation here.           
        //for each selected contact get the labels and put in collection 

        ObservableCollection<AggregatedLabelModel> contactListLabels = new ObservableCollection<AggregatedLabelModel>();

        foreach (ContactListModel contactList in contactsList.SelectedItems)
        {
            foreach (AggregatedLabelModel aggLabel in contactList.AggLabels)
            {
                contactListLabels.Add(aggLabel);
            }
        }
        //aggregate the contactListLabels by name
        ListCollectionView selectedLabelsView = new ListCollectionView(contactListLabels);

        selectedLabelsView.GroupDescriptions.Add(new PropertyGroupDescription("Name"));
        tagsList.ItemsSource = selectedLabelsView.Groups;
    }
}

解决方案

You should use an EventTrigger in combination with InvokeCommandAction from the Windows.Interactivity namespace. Here is an example:

<ListBox ...>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
            <i:InvokeCommandAction Command="{Binding SelectedItemChangedCommand}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</ListBox>

You can reference System.Windows.Interactivity by going Add reference > Assemblies > Extensions.

And the full i namespace is: xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity".

这篇关于WPF 将 UI 事件绑定到 ViewModel 中的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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