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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T00:09:55+00:00 2026-05-21T00:09:55+00:00

I have a WrapPanel that contains multiple Canvas of the same size. Each Canvas

  • 0

I have a WrapPanel that contains multiple Canvas of the same size. Each Canvas has some UIElements (i.e. TextBox, TextBlock, Buttons etc) as children. The creation of each Canvas (including its UIElement children) and the number of Canvas to be created are all done in run-time code behind (no XAML).

Initially I did the following, which works:

// declare as class properties, so all function can access them
WrapPanel wp = new WrapPanel();
Canvas[] cv = new Canvas[500];
TextBox[] tb = new TextBox[500];

// A function (e.g. a Button_Click event) that generates multiple Canvas in a WrapPanel
for (int i = 0; i<myInt; i++)
{
cv[i] = new Canvas();
tb[i] = new TextBox();
cv[i].Children.Add(tb[i]);
wp.Children.Add(cv[i]);
}

The above code is straight forwards works OK – Until I implement add, minus and destroy buttons where I could

1. Add an additional `Canvas` on a click event
2. Remove the last `Canvas` on a click event
3. Destroy a specific `Canvas` in the `WrapPanel` on a click event (may ba a little cross icon in each `Canvas`)

If I process some combination of the above 3 actions, I could easily create UIElements of the same index or create Canvas that goes out of the range of what it had been declared initially.

I looked into List however, each Canvas have different properties (each also has UIElement Children with different properties) and I can’t figure out how List would solve it. A way for me to go around that is to declare a super large Array size for Canvas (e.g. Canvas[] cv = new Canvas[99999] but I though that’s not very efficient.

Also, if I use List, how could I change properties of a specific UIElement after the they are generated? E.g. If i add 10 Canvas and add to List, and after they are all generated, I need to select the 5th Canvas and change a TextBox.Text, how do I access it like I did in an Array (i.e. tb[5].Text = “Hello”)?

Can anyone show me some approaches to this problem?

  • 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-21T00:09:55+00:00Added an answer on May 21, 2026 at 12:09 am

    Just a direct translation on how to do this with a list instead below. Given your code I don’t know why you want to keep track of the canvas and textbox’es in a list – you can just access the children collection of the WrapPanel directly instead – let’s assume you do need these separate collections for now.

     WrapPanel wp = new WrapPanel();
     List<Canvas> cvList = new List<Canvas>();
     List<TextBox> tbList = new List<TextBox>();
    
     public void Init()
    {
    
        int myInt = 500;
        // in a function (e.g. a Button_Click event) to generate the multiple Canvas in a WrapPanel
        for (int i = 0; i < myInt; i++)
        {
            Canvas cv = new Canvas();
            TextBox tb = new TextBox();
            cv.Children.Add(tb);
            wp.Children.Add(cv);
    
            cvList.Add(cv);
            tbList.Add(tb);
        }
    }
    
    
    public void AddCanvas()
    {
        Canvas cv = new Canvas();
        TextBox tb = new TextBox();
        cv.Children.Add(tb);
        wp.Children.Add(cv);
        cvList.Add(cv);
        tbList.Add(tb);
    }
    
    public void RemoveCanvas()
    {
            wp.Children.RemoveAt(wp.Children.Count-1);
            cvList.RemoveAt(cvList.Count - 1);
            tbList.RemoveAt(cvList.Count - 1);
    }
    

    Edit for added comment:

    E.g. If i add 10 Canvas, and after
    they are all generated, I need to
    select the 5th Canvas and change a
    TextBox.Text, how do I access it like
    I did in an Array (i.e. tb[5].Text =
    “Hello”)?

    You can just access the children directly. You know you only added Canvas elements to your WrapPanel. So you could do (wp is the WrapPanel again):

    TextBox textbox = (wp.Children[5] as Canvas).Children[0] as TextBox;
    textbox.Text = "Hello";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WrapPanel that has a button as a child element. Both the
I have a list of controls that I am displaying via a WrapPanel and
I have a xaml code: <Grid> <WrapPanel> <TextBox ></TextBox> <Button Content=GetIt /> </WrapPanel> </Grid>
Is there a virtualizing WrapPanel available? Commercial or free ones. I have seen some
I have a wrappanel that acts as a container for a group of controls
I have the following code that should display some information about ContactLists in a
I have 2 listboxes in my app that use <Window.Resources> <ItemsPanelTemplate x:Key=WrapPanelTemplate> <WrapPanel Width=290/>
I´m facing this problem: I have a Button that works like a WrapPanel using
I have a simple WrapPanel which contains a number of wide controls. When I
I have a List Box that implements its ItemsPanelTemplate as a WrapPanel. I need

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.