双向绑定视图的DependencyProperty到视图模型的财产? [英] Twoway-bind view's DependencyProperty to viewmodel's property?

查看:131
本文介绍了双向绑定视图的DependencyProperty到视图模型的财产?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

多种来源告诉我们,在 MVVM ,观点和的ViewModels之间的通信/同步应该通过依赖属性发生。如果我理解正确这,视图的依赖属性应绑定到采用双向绑定的视图模型的属性。现在,类似的问题已经被问过,但没有足够的答案

Multiple sources on the net tells us that, in MVVM, communication/synchronization between views and viewmodels should happen through dependency properties. If I understand this correctly, a dependency property of the view should be bound to a property of the viewmodel using two-way binding. Now, similar questions have been asked before, but with no sufficient answer.

在我开始分析这个相当复杂的问题,这是我的问题:

Before I start analyzing this rather complex problem, here's my question:

如何同步的自定义的视图的的DependencyProperty 与视图模型的属性?

How do I synchronize a custom view's DependencyProperty with a property of the viewmodel?

在一个理想的世界,你会简单地绑定它作为这样的:

In an ideal world, you would simply bind it as this:

<UserControl x:Class="MyModule.MyView" MyProperty="{Binding MyProperty}">

这是因为 myProperty的不工作是不是用户控件中的一员。卫生署!我曾尝试不同的方法,但没有证明是成功的。

That does not work since MyProperty is not a member of UserControl. Doh! I have tried different approaches, but none proved successful.

一个解决方案是定义一个基础类, UserControlEx ,必要依赖属性得到上述工作。不过,这很快就变得非常凌乱。还不够好!

One solution is to define a base-class, UserControlEx, with necessary dependency properties to get the above to work. However, this soon becomes extremely messy. Not good enough!

推荐答案

我用Caliburn.Micro从视图分离视图模型。尽管如此,它可能工作在MVVM方式相同。我猜MVVM设置视图的的DataContext 属性视图模型的实例,无论是。

I use Caliburn.Micro for separating the ViewModel from the View. Still, it might work the same way in MVVM. I guess MVVM sets the view's DataContext property to the instance of the ViewModel, either.

// in the class of the view: MyView
public string ViewModelString // the property which stays in sync with VM's property
{
    get { return (string)GetValue(ViewModelStringProperty); }
    set
    {
        var oldValue = (string) GetValue(ViewModelStringProperty);
        if (oldValue != value) SetValue(ViewModelStringProperty, value);
    }
}

public static readonly DependencyProperty ViewModelStringProperty =
    DependencyProperty.Register(
        "ViewModelString",
        typeof(string),
        typeof(MyView),
        new PropertyMetadata(OnStringValueChanged)
        );

private static void OnStringValueChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
    // do some custom stuff, if needed
    // if not, just pass null instead of a delegate
}    

public MyView()
{
    InitializeComponent();
    // This is the binding, which binds the property of the VM
    // to your dep. property.
    // My convention is give my property wrapper in the view the same
    // name as the property in the VM has.
    var nameOfPropertyInVm = "ViewModelString"
    var binding = new Binding(nameOfPropertyInVm) { Mode = BindingMode.TwoWay };
    this.SetBinding(SearchStringProperty, binding);
}



VM



VM

// in the class of the ViewModel: MyViewModel
public string ViewModelStringProperty { get; set; }

请注意,这类型的实现没有完全实施的 INotifyPropertyChanged的接口。你需要正确地更新该代码。

Note, that this kind of implementation lacks completely of implementation of the INotifyPropertyChanged interface. You'd need to update this code properly.

这篇关于双向绑定视图的DependencyProperty到视图模型的财产?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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