开发者

Accessing parent control from a Silverlight View

开发者 https://www.devze.com 2023-02-28 11:34 出处:网络
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 Pare

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)Paren开发者_开发知识库t).myScrollbar.ScrollToVerticalOffset(0);

I receive the following error:

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号