none
Access User Control elements in class (or other user user control or in the same user control but in static void) wpf RRS feed

  • Question

  • How can I access User Control elements in class (or other user user control or in the same user control but in static void)? When I want to access void or element from MainWindow i am using            
    MainWindow mW = Application.Current.MainWindow as MainWindow; mW.myTextBlock = ... or mW.SayHello();

    Is there a similar way for User Control?

    Thursday, August 15, 2019 5:10 PM

All replies

  • How can I access User Control elements in class (or other user user control or in the same user control but in static void)? When I want to access void or element from MainWindow i am using            
    MainWindow mW = Application.Current.MainWindow as MainWindow; mW.myTextBlock = ... or mW.SayHello();

    Is there a similar way for User Control?


    Hi      Developer1789,

    You can access MainWindow using the Application.Current.MainWindow method. Then, if user control is wrapped in the MainWindow active content, you can use the Application.Current.MainWindow.Content to get the active content.

    Usually, we will use the VisualTreeHelper to find controls(user control) by name or type.

    For more detailed:

    How can I find WPF controls by name or type?


    Best regards

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, August 16, 2019 2:26 AM
    Moderator
  • Hello, thank you for your respond. I tried this var holder = (Grid)Application.Current.MainWindow.FindName("functionHolder"); with your code, but every time holder = null (this holder is child of my UserControl). What I am doing wrong?


    Friday, August 16, 2019 9:52 AM
  • Hello, thank you for your respond. I tried this var holder = (Grid)Application.Current.MainWindow.FindName("functionHolder"); with your code, but every time holder = null (this holder is child of my UserControl). What I am doing wrong?
    Saturday, August 17, 2019 5:39 PM

  • Hi  Developer1789,

    >>Hello, thank you for your respond. I tried this var holder = (Grid)Application.Current.MainWindow.FindName("functionHolder"); with your code, but every time holder = null (this holder is child of my UserControl). What I am doing wrong?

    If the holder is child of your UserControl, you need to get the UserControl first.

               //get the UserControl first
                var userControlt = (UserControl1)Application.Current.MainWindow.FindName("testgf");
    
                //this holder is child of my UserControl
                var holder = (Grid)userControlt.FindName("testgrid");
                MessageBox.Show(holder.Name);



    Best regards

    Yong Lu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, August 20, 2019 1:28 AM
    Moderator