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:
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}">
That does not work since MyProperty
is not a member of UserControl
. Doh! I have tried different approaches, but none proved successful.
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!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…