Silverlight 3.0 - How to access a MainPage control value from an UserControl

别说谁变了你拦得住时间么 提交于 2020-02-03 07:16:05

问题


I need to retrieve some control values from the MainPage to an UserControl. In this UserControl I need to be able to get the Frame.ActualWidth & Frame.ActualHeight values (in this case, the Frame element is in the MainPage and the UserControl is loaded inside a MainPage's Grid via xaml). Does someone have a sample? Thank you

Josimari Martarelli ESL Sistemas Logísticos Silverlight UI Design

jmartarelli@logfacil.com.br


回答1:


MainPage m = (MainPage)Application.Current.RootVisual;




回答2:


In instances like this I'll often use have my MainPage class have a public static reference to itself, Instance. I'll set it this "this" in the constructor and then when I need access to the MainPage from down in a user control I'll just call something like:

MainPage.Instance.Foo




回答3:


First I created a static method in the App class that walks up the hierarchy of parents until it finds a match based on name. This can be used for more than just MainPage. Everything in the hierarchy should be derived from the FrameworkElement class.

    public static FrameworkElement GetParentByName(FrameworkElement currentPage, 
string ParentName)
    {

        FrameworkElement fe = (FrameworkElement)currentPage.Parent;

        // Walk your way up the chain of Parents until we get a match
        while(fe.GetType().Name != ParentName)
            fe = (FrameworkElement)fe.Parent;

        return fe;

    }

Then to use this I just call something like:

MainPage m = (MainPage)App.GetParentByName(this, "MainPage");




回答4:


If you are using Login page then you have to cast using your login page.

i.e Login lp = (Login)Application.Current.RootVisual;
You can use the parameter that you created in login page.



来源:https://stackoverflow.com/questions/1728879/silverlight-3-0-how-to-access-a-mainpage-control-value-from-an-usercontrol

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!