views:

27

answers:

1

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.

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();
    }
}
+3  A: 

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.

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

(see also http://caliburnmicro.codeplex.com/wikipage?title=All%20About%20Actions)

Marco Amendola