本文介绍了WPF UI绑定事件的命令在视图模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个简单的应用程序遵循MVVM一些重构和我的问题是我怎么移动SelectionChanged事件我的code的后面视图模型?我看着结合元素命令一些例子,但并没有完全掌握它。任何人都可以协助这一点。谢谢!

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

 公共部分类MyAppView:窗口
{
    公共MyAppView()
    {
        的InitializeComponent();        this.DataContext =新MyAppViewModel();        需要下面这点创建对象//将code。
    }    私人无效contactsList_SelectionChanged(对象发件人,System.Windows.Controls.SelectionChangedEventArgs E)
    {
        // TODO:添加事件处理程序实现在这里。
        //为每个选定的联系人获得的标签,并把收集中        的ObservableCollection< AggregatedLabelModel> contactListLabels =新的ObservableCollection< AggregatedLabelModel>();        的foreach(ContactListModel contactList在contactsList.SelectedItems)
        {
            的foreach(AggregatedLabelModel aggLabel在contactList.AggLabels)
            {
                contactListLabels.Add(aggLabel);
            }
        }
        //汇总按名称contactListLabels
        的ListCollectionView selectedLabelsView =新的ListCollectionView(contactListLabels);        selectedLabelsView.GroupDesc​​riptions.Add(新PropertyGroupDesc​​ription(姓名));
        tagsList.ItemsSource = selectedLabelsView.Groups;
    }
}


解决方案

您应该使用的EventTrigger InvokeCommandAction 从Windows.Interactivity命名空间。下面是一个例子:

 < ListBox中...>
    < I:Interaction.Triggers>
        < I:的EventTrigger事件名称=的SelectionChanged>
            < I:InvokeCommandAction命令={结合SelectedItemChangedCommand}/>
        < /我:&的EventTrigger GT;
    < /我:Interaction.Triggers>
< /列表框>

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>

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

09-27 14:48