双向绑定视图的 DependencyProperty 到视图模型的属性? [英] Twoway-bind view's DependencyProperty to viewmodel's property?
问题描述
网络上的多个来源告诉我们,在 MVVM
中,视图和视图模型之间的通信/同步应该通过依赖属性发生.如果我理解正确,应该使用双向绑定将视图的依赖属性绑定到视图模型的属性.现在,以前也有人问过类似的问题,但没有足够的答案.
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
不是 UserControl
的成员.呸!我尝试了不同的方法,但都没有成功.
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 将 ViewModel 与 View 分开.尽管如此,它可能在 MVVM 中以相同的方式工作.我猜 MVVM 也将视图的 DataContext
属性设置为 ViewModel 的实例.
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);
}
虚拟机
// 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屋!