WPF Usercontrol interaction with parent view / viewmodel
Posted
by obaylis
on Stack Overflow
See other posts from Stack Overflow
or by obaylis
Published on 2010-06-17T16:52:44Z
Indexed on
2010/06/17
17:03 UTC
Read the original article
Hit count: 560
Hi I have a mainView window which has its dataContext set to it's own viewModel.
On that viewModel is a DateTime property which in turn is bound to a datepicker on my main view using 2 way binding.
<toolkit:DatePicker DateSelected="{Binding mainDateTimeProperty, Mode=TwoWay}" />
This is all fine so far. On the change of my datetime property I create a list which is then bound to a datagrid elsewhere on the mainview. This all works fine.
My question is to do with a usercontrol I want to add to the main view. I want this usercontrol to be self contained so have created it with it's own viewmodel but it does also need access to mainDateTimeProperty
I thought that best way to go would be to create a dependencyProperty on the usercontrol and when I create my control in the main view I bind the dp to the datetime as follows.
<uc:MyNewUserControl DateProperty="{Binding mainDateTimeProperty}" />
Trouble is how do I have the usercontrol maintain datacontext with it's viewmodel and yet still have the dependency property bound to a property on the main view model?
Hope this is clear. Can post some more code if necessary. Looking for a best practice approach if possible.
Thanks very much for any advice.
© Stack Overflow or respective owner