I have struggled to find a good solution to these problems as well.
I would love to be able to create a dependency property on child control, then
in xaml on the parent bind the parent datacontext (or whatever property I like)
to the dependency property of the child.
Then, in my dream land in the child control I could detect the dependency
property has changed (eg in the setter or event) and refresh the child
control in some way or another. This would give a nicely loose coupled
solution.
The solution Miguel has given works, but it has to compromise a lot of things
to achieve the situation where the child can discover the parents
datacontext.
I think If you are creating the control in code behind only, then you may as
well pass through the viewmodel in the child control constructor. As
Miguel points out, you have a race condition if you try and create the control
in xaml, as the parent's context might not be set at the time the child control
is constructed and it's bindings occur.
The other problem with a UserControl in this manner is you lose the ability to
use the control in Blend. Although I struggle to do that where I wonder
if it is better not to use a data driven user controls if you want to style in
Blend.
Perhaps the cleanest solution in the current architecture is to use
DataTemplates and styles in place of user controls where possible. If
this isn't possible, I favour doing it an a traditional manner, where the
parent passes in the dependencies (be it a viewmodel or an id so it can create
it's own view model).
I don't like the idea of the child scavenging through its parent looking for a
dependency.
On Mon, 25 Jan 2010 06:25:30 +0530 wrote
>Attached is a quick sample on how to do this. All the problem is timing
since the parent is set after the constructor and the Xaml is parsed in the
ChildControl. Check the comments on ChildControl.xaml Unless you override
the DataContext, the ChildControl should've the same as the parent and binding
should just work. Then you can access it in Codebehind (again after Loaded due
to the same reason I mentioned)
>
> On Mon, Jan 25, 2010 at 10:50 AM, <[email protected]> wrote:
>Hi,
>
>Apologies for the change of title - I don't have access to the original
emails. Thanks to the people
>who have replied so far.
>
>I am still having problems with trying to access page resources from a user
control.
>
>If I put this withing the control:
>
>
>
>
>where vm aliases the namespace:
xmlns:vm="clr-namespace:MyCustomer.MyProject.ViewModel"
>
>Then I can access the ViewModel object, however it's a newly instantiated
instance of that view
>model object.
>
>My preference is to reuse the existing one that exists on the parent Page,
as that one already has
>data loaded that I want to use, hence less unnecessary database requests.
>
>fyi, the price view model object is declared in the xaml as:
>
>
>
>
>This exists in the constructor of the page (after InitializeComponent),
and, as expected, is not null.
>So the following line
>moPriceViewModel = Resources["PriceViewModel"] as PriceViewModel;
>is set correctly.
>
>Adding the DataContext did nothing. I added it in the xaml in both the page
and in the control root
>element, then I tried adding the view model object in the code behind on
the parent page, but it
>still came through null.
>
>The datatype of the Parent property is DependencyObject, and it is null
when I attempt to access it
>from within the constructor of the control (after the InitializeComponent
statement), even though
>the control exists in the xaml of the parent. Help suggests that I need to
set the Parent property by
>adding the control to a collection. I did that explicitly (although I
believe it should already be set
>within the xaml), adding it to the layout grid, but Parent still came
through null.
>
>I also tried
>ManageTermsPage mtp = (ManageTermsPage)VisualTreeHelper.GetParent(this);
>but I think this failed for the same reason - Parent property is null.
>
>If I could get the DataContext working, passing the datasource through from
the page to the
>control, I would be happy with that.
>
>I do need access to the resource from within the xaml and in the
code-behind.
>
>Any other ideas on how I might achieve this?
>
>Regards,
>Tony
>
>_______________________________________________
>ozsilverlight mailing list
>[email protected]
>http://prdlxvm0001.codify.net/mailman/listinfo/ozsilverlight
>
>--
>Miguel A. Madero Reyes
>www.miguelmadero.com (blog)
>[email protected]
>_______________________________________________
>ozsilverlight mailing list
>[email protected]
>http://prdlxvm0001.codify.net/mailman/listinfo/ozsilverlight
>
_______________________________________________
ozsilverlight mailing list
[email protected]
http://prdlxvm0001.codify.net/mailman/listinfo/ozsilverlight