I am looking for an example of how to implement the property IsEnabledChanged of the dataform using MVVM light, I set up the triggers but not sure about the implementation. So,
I created a Behavior that does something similar with EditStarted event of a data form.
public class EditableDataFormBehavior : Behavior
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.Loaded += AssociatedObject_Loaded;
}
void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
{
AssociatedObject.CurrentItemChanged += AssociatedObject_CurrentItemChanged;
}
void AssociatedObject_CurrentItemChanged(object sender, EventArgs e)
{
var editableObject = AssociatedObject.CurrentItem as INotifyEditableObject;
if (editableObject != null)
{
editableObject.EditStarted += Object_EditStarted;
}
}
void Object_EditStarted(object sender, EventArgs e)
{
AssociatedObject.BeginEdit();
}
protected override void OnDetaching()
{
// detach the event handler
AssociatedObject.Loaded -= AssociatedObject_Loaded;
AssociatedObject.CurrentItemChanged -= AssociatedObject_CurrentItemChanged;
var editableObject = AssociatedObject.DataContext as INotifyEditableObject;
if(editableObject!=null)
editableObject.EditStarted -= Object_EditStarted;
base.OnDetaching();
}
}
You would just need to modify slightly by attaching to the IsEnabledChanged event instead. You are trying to achieve a view only behavior and the viewmodel should know nothing about it. Main benefit doing it this way is that it is more reusable than code behind.
In the event handler you would add your code:
if (!IsEnabled)
{
VisualStateManager.GoToState(DataForm, "Disabled", true);
}
else
{
VisualStateManager.GoToState(DataForm, "Normal", true);
}
Attach the behavior to the data form (mine was made for telerik's one but should be doable with the sdk)