Accessing parent control from a Silverlight View

3.3k Views Asked by At

I have a scrollbar on my parent UserControl in a Silverlight 4 Business application that I would like to access from a View; I want to simply tell it to scroll to the top. I have tried casting the Parent as the type of the "MainPage", but this does not seem to work. How should I be doing this?

This doesn't work:

((MainPage)Parent).myScrollbar.ScrollToVerticalOffset(0);

I receive the following error:

Unable to cast object of type 'System.Windows.Controls.Frame' to type 'Project.MainPage'.

1

There are 1 best solutions below

1
On BEST ANSWER

It's because your scrollbar's parent control is a frame and not MainPage. You should go through page hierarchy and find main page with its ID, get that control and cast it to MainPage. You should be able to do this afterwards.

[EDIT]

So, to do what you need, here are few solutions.

Recursive utility function to find parent control: http://forums.silverlight.net/forums/t/55369.aspx

Raise an event on parent control: http://forums.silverlight.net/forums/t/55458.aspx

Datacontext binding: not sure for UI elements: Calling parent UserControl method from a child UserControl in Silverlight