Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6924471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:40:02+00:00 2026-05-27T10:40:02+00:00

It seems strange but I cast some custom WPF User Controls to WPF UserControl

  • 0

It seems strange but I cast some custom WPF User Controls to WPF UserControl and I have to call some method which have all of them. Is it possible? How to do it?

foreach (var kid in ((MainWindow)App.Current.Windows[0]).MainCanvas.Children)
{
string kidType = kid.GetType().FullName;
if (kidType.EndsWith("MyUserControl"))
{
    UserControl myUserControl = (UserControl)kid;

    myUserControl.Hide() // <- this method I want to call bu it is "hidden" because of teh casting which doesn't provide access to it.

Note: The method Hide() is public.

SOLUTION:

Hi all!

Thanks for your input! Finally I got the solution.

foreach (var kid in ((MainWindow)App.Current.Windows[0]).MainCanvas.Children)
{
  string kidType = kid.GetType().FullName;
  if (kidType.EndsWith("UControl"))
  {
     Type t = kid.GetType();
     object obj = Activator.CreateInstance(t);
     t.InvokeMember("Hide", BindingFlags.Default | BindingFlags.InvokeMethod, null, obj, new object[] { });

      // And here there is a 1000% better solution of @Erno
      // dynamic myUserControl = kid;
      // myUserControl.Hide();
   }
}

where is

public void Hide()
{
    // do stuff
} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:40:03+00:00Added an answer on May 27, 2026 at 10:40 am

    dynamic might be useful to prevent writing reflection code (See this):

    foreach (var kid in ((MainWindow)App.Current.Windows[0]).MainCanvas.Children) {
        string kidType = kid.GetType().FullName;
        if (kidType.EndsWith("MyUserControl"))
        {
            dynamic myUserControl = kid;
            myUserControl.Hide();  
        } 
    }
    

    Make sure you add some exception handling just in case you encounter a type that fits the name but doesn’t implement Hide.

    If you are worried about that you could implement an interface on all the UserControls and try-cast to that interface instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems very strange to me but I have a script that I'm running
The following seems strange.. Basically, the somedata attribute seems shared between all the classes
I ran across this case of UnboundLocalError recently, which seems strange: import pprint def
The problem seems very strange. I have a AJAX helper function within a same
It seems I tend to attract strange issues. This time, I have written a
SPListItem.GetFormattedValue seems to have a strange behavior for DateTime fields. It retrieves the DateTime
I have a strange case with SWT and Button after using setEnabled() - seems
See: http://jsbin.com/ebudec/edit#javascript,html It seems strange but I think jsBin doesn't recognize the jQuery document
An exception was thrown in my application, but the HTTP_REFERER was null, which seems
While refactoring some code, I came across this strange compile error: The constructor call

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.