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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:14:56+00:00 2026-05-20T05:14:56+00:00

Hell all, I am currently trying to create a few object dynamically in code.

  • 0

Hell all, I am currently trying to create a few object dynamically in code. This has been successful up to the point to where I want to identify the element for example
Here I create the object ( an ellipse with a drop shadow)

public MainPage()
    {
        InitializeComponent();
        Loaded += new RoutedEventHandler(MainPage_Loaded);
    }

    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        var element = CreateEllipse();
        LayoutRoot.Children.Add(element);
    }

    public Ellipse CreateEllipse()
    {
        StringBuilder xaml = new StringBuilder();
        string ns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
        xaml.Append("<Ellipse ");
        xaml.Append(string.Format("xmlns='{0}'", ns));
        xaml.Append(" x:Name='myellipse'"); //causes the exception
        xaml.Append(" Margin='50 10 50 10'");
        xaml.Append(" Grid.Row='0'");
        xaml.Append(" Fill='#FD2424FF'");
        xaml.Append(" Stroke='Black' >");
        xaml.Append("<Ellipse.Effect>");
        xaml.Append("<DropShadowEffect/>");
        xaml.Append(" </Ellipse.Effect>");
        xaml.Append(" </Ellipse>");
        var ellipse = (Ellipse)XamlReader.Load(xaml.ToString());
        return ellipse;
    }

What I want to do is after the object is created I want to be able to locate the parent objects using VisualTreeHelper.

public void button1_Click(object sender, RoutedEventArgs e)
    {
        DependencyObject o = myellipse;
        while ((o = VisualTreeHelper.GetParent(o)) != null)
        {
            textBox1.Text = (o.GetType().ToString());
        }
    }

Can anyone point me in the right direction for referencing a dynamically created object in a scenario like this or how to properly define x:Name for an object programatically?

Thank you,

  • 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-20T05:14:56+00:00Added an answer on May 20, 2026 at 5:14 am

    Can anyone point me in the right direction for referencing a dynamically created object in a scenario like this or how to properly define x:Name for an object programatically?

    You can’t reference “myellipse” directly like this with dynamically generated types. The compiler converts the x:Name to a type at compile time – since you’re loading this at runtime, it will not exist.

    You could, instead, make a “myellipse” variable, and have your dynamic generation routine set it:

    void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        var element = CreateEllipse();
        LayoutRoot.Children.Add(element);
    }
    
    private Ellipse myEllipse; 
    public Ellipse CreateEllipse()
    {
        StringBuilder xaml = new StringBuilder();
        string ns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
        xaml.Append("<Ellipse ");
        xaml.Append(string.Format("xmlns='{0}'", ns));
        xaml.Append(" Margin='50 10 50 10'");
        xaml.Append(" Grid.Row='0'");
        xaml.Append(" Fill='#FD2424FF'");
        xaml.Append(" Stroke='Black' >");
        xaml.Append("<Ellipse.Effect>");
        xaml.Append("<DropShadowEffect/>");
        xaml.Append(" </Ellipse.Effect>");
        xaml.Append(" </Ellipse>");
        this.myEllipse = (Ellipse)XamlReader.Load(xaml.ToString());
    
        return this.myEllipse;
    }
    

    That being said, I would recommend not building the ellipse using strings like this. You could just create the ellipse class directly, and add the effect. You don’t need to use XamlReader to parse a string in this case.

    private Ellipse myEllipse; 
    public Ellipse CreateEllipse()
    {
         this.myEllipse = new Ellipse();
         this.myEllipse.Effect = new DropShadowEffect();
         Grid.SetRow(this.myEllipse, 0);
         // Set properties as needed
    
         return this.myEllipse;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If this is answered somewhere else, kindly smack me and point me in the
Well I'm having a hell of a time trying to get my CPU down
I am trying to figure out the right workflow for this situation: On the
Hell-o all, Im new to qt and I am having trouble drawing one single
I'm trying to create a Snake Game where the movement of the snake isn't
I am currently implementing something quite similar to checkers. So, I have this table
Microsoft seems hell-bent on deprecating the swiss-army-knife of database tools. What else comes close
What is classpath hell and is/was it really a problem for Java?
I'm having a hell of a time understanding pointers in Objective C. They don't
I wonder, why the hell... did the VS team consider that NOT finding a

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.