binding to a method on a control

Feb 3, 2011 at 10:53 PM

This might be more of a general mvvm question, but I can't find anything helpful. I need to call DataGrid.BeginEdit() when certain code in the ViewModel executes and changes a property on an entity. The property may or may not be bound to a column, but I still need the row to go into edit mode so that it properly handles events related to CurrentItem. Any suggestions?

Feb 3, 2011 at 11:37 PM

If your view-model can expose a propery used to determine if it should be in edit mode, you can create a proper attached behaviour (through an attached property), to call BeginEdit().

public static readonly DependencyProperty IsInEditModeProperty = DependencyProperty.RegisterAttached("IsInEditMode",
                                                                 new FrameworkPropertyMetadata(false, OnIsInEditModePropertyChanged));

private static void OnMyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)

Supposing that a change of a property will trigger the change to the VM property bound to the above attached property, you should get the required behaviour.


Another chance could be to create a custom behaviour using a TriggerBase-derived object. Such trigger could react to required property change events, invoking the BeginEdit directly.

The only thing I don't like about the second approach is that you are forced to put some VM logic into a trigger (which lives in the view domain). That's why I would go with the first option, since it keeps view and view-model nicely decoupled.

Feb 4, 2011 at 6:51 PM


With your first suggestion, and a nice generic implementation of IEditableObject and IRevertibleChangeTracking, I should be set.