使用Caliburn.Micro将绑定选择更改为ViewModel [英] Binding SelectionChanged to ViewModel using Caliburn.Micro

查看:200
本文介绍了使用Caliburn.Micro将绑定选择更改为ViewModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在一个新的Silverlight项目中使用了Caliburn.Micro,一切正常。内置的约定将按钮单击事件绑定到viewModel,但是我不确定处理数据网格和组合框上的selectionChanged事件的最佳方法是什么。

We've using Caliburn.Micro on a new Silverlight project and everythings working great. The inbuilt conventions bind buttons click events to the viewModel, but I'm not sure what the best way to handle the selectionChanged event on datagrids and comboboxes is.

目前,我绑定到所选项目并调用自定义逻辑,但是我感觉这有点代码味道,我应该将属性的设置和selectedChange事件分开。但是,如果我将它们分开,如何通过命令将选择更改的事件绑定到我的viewModel?还是EventTrigger?还是下面的代码可以接受?这是一个很小的变化,但我到处都采用这种逻辑。

At the moment, I'm binding to the selected item and calling custom logic, but I feel like this is a bit of a code smell and that I should seperate the setting of the property and the selectedChange event. But if I seperate these, how do I bind the selection changed event to my viewModel, by commands? or an EventTrigger? Or is the code below acceptable? Its a small change but I do this logic everywhere.

private Foo _selectedFoo;
public Foo SelectedFoo
{
    get
    {
        return _Foo;
    }
    set
    {
        if (_Foo != null && _Foo.Equals(value)) return;
        _Foo = value;
        NotifyOfPropertyChange("SelectedFoo");
        NotifyOfPropertyChange("CanRemove");
        LoadRelatedBars();
    }
}


推荐答案

I定期使用此技术,我对此非常满意。

我发现VM对自己的状态变化做出反应非常好,不需要外部参与者(顺便说一下是View,但也可能是另一个组件)来设置新状态THEN

I use this technique regularly and I feel very comfortable with it.
I find perfectly fine that the VM reacts to its own state change, without the need for the external actor (which incidentally is the View, but could be another component, too) to set the new state, THEN signal the VM that the state is changed.

但是,如果确实要,可以使用Message.Attach附加属性将View中的事件挂接到VM中的操作:

If you really want to, however, you can use the Message.Attach attached property to hook an event in the View to an action in the VM:

cal:Message.Attach="[Event SelectionChanged] = [OnSelectionChangedAction]"

(另请参见 https://caliburnmicro.com/documentation/actions

这篇关于使用Caliburn.Micro将绑定选择更改为ViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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