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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:19:05+00:00 2026-05-26T05:19:05+00:00

I need to dynamically add the controls to a window. For example for a

  • 0

I need to dynamically add the controls to a window. For example for a comboBox, I need to map with the Custom Framework which will map the Lookup to that comboBox…

So whenever the combobox is mapped with lookup, I need to Add a (+) Button to the right side of the comboBox.. This (+) button is used to open a respective lookup form to add an item to the comboBox.

For that I need to do the following..

  1. In Mapper I need to create instance for Button Label with (+),
  2. I need to create a Panel., ie., Container for holding both the Controls (ComboBox,Button)
  3. For replacing Combobox with that created panel, I need to remove the Combobox from the Parent of Combobox and add panel to that control.

How can we do this in WPF? ie. dynamically replacing controls in Code behind.

Thanks in advance, Dinesh

  • 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-26T05:19:06+00:00Added an answer on May 26, 2026 at 5:19 am

    I would suggest using MVVM pattern with separation of View and ViewModel. To solve your issue you can use DataTemplates along with a DataTemplateSelector. Basically you should prepare different Data templates and depends on internal state of applicaiton / a particular ViewModel switch Views (XAML layouts).

    1) Define all templates in resources:

       <!-- namespace where GridView .xaml and ChartView .xaml are -->           
       xmlns:views="clr-namespace:Gui.Views"
       ...
    
       <DataTemplate x:Key="GridDataTemplate">
            <views:GridView />
        </DataTemplate>
        <DataTemplate x:Key="ChartDataTemplate">
            <views:ChartView />
        </DataTemplate>
    

    2) Specify selector in Control’s presenter:

    <Control....
        <ContentPresenter 
           ContentTemplateSelector="{StaticResource YourDataTemplateSelector}" />
    

    3) Define selector in View’s code behind:

     private sealed class YourDataTemplateSelector: DataTemplateSelector
     {            
          public override DataTemplate SelectTemplate(
                                          object item, 
                                          DependencyObject container)
            {
                DataTemplate dataTemplate = null;
                IViewModel viewModel = item as IViewModel;
                if (viewModel.ViewType == Grid)
                {
                   dataTemplate = this.Resources["GridDataTemplate"];
                }
    
                // ...
                return dataTemplate;                
            }
     }
    

    EDIT: Dynamically build controls

    • You can use VisualTreeHelper.GetChild() to traverse controls tree,
      find children controls, check whether they are of specific type and then remove/update
    • You can reffer parent control by control.Parent property
    • You can remove any child control using (parent as Panel).Children.Remove()
    • You can add control using (parent as Panel).Children.Add()
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a GridView that I need to dynamically add TemplateField elements to. My
I have a table of data that I need to dynamically add a column
I have a dataGrid(not dataGridView) in which i need to add Checkbox dynamically at
I have a TabBar Control with 3 tab, i need to add controls(Button,Label) dynamically
I need to add some controls dynamically, As the number of elements in my
I'm dynamically loading User Controls into a div, which I need to preserve on
I need to dynamically create a Video object in ActionScript 2 and add it
I need to add css height to each page dynamically with jQuery. So far
I need to create several Divs dynamically, and I need to add onmouseover event
I am trying to add a bunch of controls dynamically in rows and columns

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.