如何使用MVVM实现Silverlight数据形式的IsEnabledChanged [英] How to implement IsEnabledChanged of silverlight dataform using MVVM

查看:134
本文介绍了如何使用MVVM实现Silverlight数据形式的IsEnabledChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个如何使用MVVM light实现数据表单的属性IsEnabledChanged的示例,我设置了触发器,但不确定实现.因此,这是版本背后的代码:

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, here is the code behind version:

  void DataForm_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        if (!IsEnabled)
        {
            VisualStateManager.GoToState(DataForm, "Disabled", true);
        }
        else
        {
            VisualStateManager.GoToState(DataForm, "Normal", true);
        }
    }

DataForm.IsEnabledChanged += new DependencyPropertyChangedEventHandler(DataForm_IsEnabledChanged);

在使用MVVM光的xaml中,我这样做:

in xaml using MVVM light I did this:

 <i:EventTrigger EventName="IsEnabledChanged">
                    <cmdextras:EventToCommand Command="{Binding IsEnabledChangedCommand}" CommandParameter="{Binding .}" />
                </i:EventTrigger>

现在,需要弄清楚如何翻译后面的代码以使用relaycommand.

Now, need to figure out how to translate the code behind to use the relaycommand.

推荐答案

我创建了一个Behavior(行为),该行为与数据表单的EditStarted事件类似.

I created a Behavior that does something similar with EditStarted event of a data form.

public class EditableDataFormBehavior : Behavior<RadDataForm>
{
    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();
    }


}

您只需要通过附加到IsEnabledChanged事件来稍作修改即可.您正在尝试实现仅视图行为,而视图模型对此一无所知.这样做的主要好处是,它比后面的代码更可重用.

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);
    }

将行为附加到数据表单(我的代码是为telerik编写的,但应该可以与sdk一起使用)

Attach the behavior to the data form (mine was made for telerik's one but should be doable with the sdk)

 <i:Interaction.Behaviors>
                <utilities:EditableDataFormBehavior/>
 </i:Interaction.Behaviors>

这篇关于如何使用MVVM实现Silverlight数据形式的IsEnabledChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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