Why does IViewFor<T> not use DataContext

346 views
Skip to first unread message

Steven Kirk

unread,
Nov 21, 2013, 1:39:17 PM11/21/13
to reacti...@googlegroups.com
Implementing the IViewFor<T>.ViewModel propery and keeping it in sync with the DataContext requires a fair bit of boilerplate (DP, property accessor, change handler). Why is the separate ViewModel property necessary?

Paul Betts

unread,
Nov 21, 2013, 3:18:55 PM11/21/13
to ReactiveUI mailing list
Hey Steven,

The ViewModel property is necessary because DataContext doesn't exist on many platforms, and the ReactiveUI way is to use RxUI bindings instead of XAML bindings, which don't care about DataContext (see https://github.com/reactiveui/ReactiveUI/blob/docs/docs/basics/bindings.md for the details).

However, should you want to sync ViewModel with DataContext, it is pretty easy to do so, just drop this line in your constructor:

this.WhenAnyValue(x => x.ViewModel).BindTo(this, x => x.DataContext);

Paul


On Thu, Nov 21, 2013 at 10:39 AM, Steven Kirk <gro...@gmail.com> wrote:
Implementing the IViewFor<T>.ViewModel propery and keeping it in sync with the DataContext requires a fair bit of boilerplate (DP, property accessor, change handler). Why is the separate ViewModel property necessary?

--
You received this message because you are subscribed to the Google Groups "ReactiveUI mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to reactivexaml...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Steven Kirk

unread,
Nov 21, 2013, 4:56:17 PM11/21/13
to reacti...@googlegroups.com
Thanks Paul, that makes total sense. And I hadn't considered using a binding like that, very neat! 

ReactiveUI is great but needs more documentation of basic things like this. Maybe time to reinstate my long dormant blog...
Reply all
Reply to author
Forward
0 new messages