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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:34:54+00:00 2026-06-16T21:34:54+00:00

I want to create a user control which would work the same way as

  • 0

I want to create a user control which would work the same way as a classic Panel (or Canvas rather) control expect that I want to have there some default Buttons which the user won’t be able to remove.

I tried this:

namespace WpfApplication1
{
    public class CustomPanel : Canvas
    {
        public CustomPanel()
        {
            Button b = new Button();
            b.Name = "Button1";
            b.Content = "Button1";
            this.Children.Add(b);
        }
    }
}

It works, but when I compile it and create an instance of CustomPanel in the designer and then try to insert another item the Button created in constructor gets deleted.

Is this the correct approach at all or is there a better (more effective/elegant) way then modfying the constructor?

Thanks in advance for any efforts.

  • 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-16T21:34:55+00:00Added an answer on June 16, 2026 at 9:34 pm

    Your problem is that you add the Button to the Children object, in the constructor, and then replace the whole Children object when you instance it in XAML.
    I’m guessing your XAML looks like this:?

    <wpfApplication3:CustomPanel>
       <Button Content="New b"/>
    </wpfApplication3:CustomPanel>
    

    If you initiate it like this instead, you’ll see that the button stays in place.

    public MainWindow()
    {
        InitializeComponent();
        Loaded += OnLoaded;
    }
    
    private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
    {
        CustomPanel p = new CustomPanel();
        p.Children.Add(new Button(){Content = "T"});
        gr.Children.Add(p);
    }
    

    What you can do to avoid this is this:

    public CustomPanel()
    {
        Initialized += OnInitialized;
    }
    
    private void OnInitialized(object sender, EventArgs eventArgs)
    {
        var b = new Button { Name = "Button1", Content = "Button1" };
        Children.Insert(0,b);
    }
    

    Now you wait until the XAML have replaced the Children object, before you add your button.

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

Sidebar

Related Questions

ASP.Net 3.5 I want to create a user control that contains the ListView and
I want create wordpress website into which I want create user management... That means
I want to create a user control [textbox] which convert all the text in
I have a stored procedure in which i want to create a user defined
I have been trying to create a user control library which simply has styled
I have a user control which I want to define as a template in
I want to create a live, checkers-like app, which will work like this: There
I want to create in C# Visual Studio 2010 my user control from standard
I'm writing a WPF user control which internally uses MVVM. In order to work
I want to create gallery application which would get thumbnails and metadata from web

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.