thanks Miguel, I've worked with this. It gives me access to the parent control, but unfortunately not bindings in the xaml. I tried modifying this to attempt to override a locally created datasource resource, but that didn't work either. I also experienced the unexpected exceptions with attempting to bind. So unfortunately, this hasn't really given me anything over simply passing the resource in to the user control as a property. Ideally I would love to have access to the bindings in the xaml.
T. On Mon, Jan 25th, 2010 at 11:59 AM, Miguel Madero <[email protected]> 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: > > <UserControl.Resources> > > <vm:PriceViewModel x:Key="PriceViewModel" d:IsDataSource="True" > /> > > </UserControl.Resources> > > > > 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: > > <navigation:Page.Resources> > > <vm:PriceViewModel x:Key="PriceViewModel" > d:IsDataSource="True" /> > > </navigation:Page.Resources> > > > > 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
