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

  • SEARCH
  • Home
  • 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 7841315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:04:26+00:00 2026-06-02T16:04:26+00:00

I have a UserControl named AddressTemplate , which contains a StackPanel with an assortment

  • 0

I have a UserControl named AddressTemplate, which contains a StackPanel with an assortment of Labels & Textboxes. What I need is a way to find the immediate ancestor/parent of one of the controls within the AddressTemplate. Essentially, I need a way to determine whether a given Textbox is inside the AddressTemplate, or instead is outside this UserControl and is just a standalone control.

What I’ve come up with so far is this:

private bool FindParent(Control target)
    {
        Control currentParent = new Control();

        if (currentParent.GetType() == typeof(Window))
        {

        }
        else if (currentParent.GetType() != typeof(AddressTemplate) && currentParent.GetType() != null)
        {
            currentParent = (Control)target.Parent;
        }
        else
        {
            return true;
        }

        return false;            
    }

The problem is, I keep getting an InvalidCastException because it can’t cast a StackPanel as a Control. Does anybody know the proper cast, or a viable way to fix this?

  • 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-06-02T16:04:27+00:00Added an answer on June 2, 2026 at 4:04 pm

    You probably want to use LogicalTreeHelper.GetParent here, which returns a DependencyObject:

    //- warning, coded in the SO editor window
    private bool IsInAddressTemplate(DependencyObject target)
    {
        DependencyObject current = target;
        Type targetType = typeof(AddressTemplate);
    
        while( current != null)
        {
           if( current.GetType() == targetType)
           {
              return true;
           }
           current = LogicalTreeHelper.GetParent(current);
        }
        return false;
     }
    

    This would walk up the logical parent tree until it found no parent or the user control you are looking for. For more info, look at Trees in Wpf on MSDN

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

Sidebar

Related Questions

I have user control named DateTimeUC which has two textboxes on its markup: <asp:TextBox
I have UserControl which contains a TextBox and a Button control. The Button opens
Suppose I have a Window displaying a UserControl. The UserControl contains a TextBox which
In VS2010, targeting Framework 4.0, I have a UserControl which contains a TableLayoutPanel with
I have created a UserControl name TitleBar, which is placed in every view. The
I have a usercontrol which has a couple of textblocks on it <UserControl x:Class=Tester.Messenger
I have a UserControl that consists of three TextBoxes. On a form I can
I have UserControl, named thisUserControl , it has two controls as shown in the
I have a UserControl in my project called 'UIWizard.cs', and a 24-bit Bitmap named
I have a UserControl named BreadCrumb. I would like to pass in information to

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.