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 1005693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:20:20+00:00 2026-05-16T08:20:20+00:00

A quick question regarding WPF: How can I select child elements within a Canvas/Grid?

  • 0

A quick question regarding WPF: How can I select child elements within a Canvas/Grid?

For example, in the following XAML snippet:

    <Canvas Name="parentCanvas" Width="200" Height="200" Background="AliceBlue" MouseMove="parentCanvas_MouseMove" MouseLeave="parentCanvas_MouseLeave">
        <Border Name="border1" Margin="10" Height="100" Width="180" Background="Maroon" Canvas.Top="47" />
    </Canvas>

…how can I access border1 within parentCanvas_MouseMove and parentCanvas_MouseLeave methods?

Also, the way I access the canvas itself within the event handlers looks a bit hacky, I do something along the lines of:

    private void parentCanvas_MouseLeave(object sender, MouseEventArgs e)
    {
        Canvas c = (Canvas)sender;
        c.Background = Brushes.Blue;
    }

Is that the right way of accessing it or is there a more elegant way of doing this?

Many thanks,
Hamza.

  • 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-16T08:20:21+00:00Added an answer on May 16, 2026 at 8:20 am

    When you name a control with a Name attribute, it will generate a member in the class, so you should be able to access them by name:

    private void parentCanvas_MouseLeave(object sender, MouseEventArgs e)
    {
        Canvas c = parentCanvas;
        c.Background = Brushes.Blue;
        Border b = border1;
    }
    

    If the controls are part of a ControlTemplate or DataTemplate, then you would need to get a reference to the parent and look it up in the template. For example, if your XAML is part of a DataTemplate, you would have to do something like this:

    private void parentCanvas_MouseLeave(object sender, MouseEventArgs e)
    {
        Canvas c = (Canvas)sender;
        var contentPresenter = (ContentPresenter)c.TemplatedParent;
        var b = contentPresenter.ContentTemplate.FindName(
            "border1", contentPresenter);
    }
    

    In any case, casting the sender parameter to the appropriate type is perfectly reasonable, and it lets you make your event handlers more reusable.


    If you don’t have a name for your canvas, you can also use the Children property of the Canvas to iterate through its visual children:

    private void parentCanvas_MouseLeave(object sender, MouseEventArgs e)
    {
        Canvas c = (Canvas)sender;
        var firstChild = c.Children[0];
        var firstBorderChild = c.Children.OfType<Border>().FirstOrDefault();
    }
    

    Finally, if you have a visual that isn’t a Panel, you can use VisualTreeHelper.GetChildrenCount and VisualTreeHelper.GetChild:

    for (int childIndex = 0; 
         childIndex < VisualTreeHelper.GetChildrenCount(c); 
         childIndex++)
    {
        var child = VisualTreeHelper.GetChild(c, childIndex);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i need a quick hint regarding the following exercise question: Write a program that
Quick question regarding CSS and the browser. I tried searching SO and found some
quick question regarding Array's in xcode. I have ht efollowing code, which is supposed
I havea quick question regarding hook_form. Does it work on custom content types created
Quick question. What do you think, I have a few sites that use a
Quick question: Would it be a good or a bad idea to implement my
Quick question: what is the compiler flag to allow g++ to spawn multiple instances
Quick question. There is a legacy website (that is not under my control and
Quick question, I was curious if there is any difference between a database table
Quick question (I hope), how do I include a window 6 class library dll

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.