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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:51:40+00:00 2026-05-28T03:51:40+00:00

I have a customcontrol, need to be added as many times when clicing on

  • 0

I have a customcontrol, need to be added as many times when clicing on a button. This has to achived from MVVM WPF pattern. i have pasted my code here. It will be great if you guys can help on this.

Please help me

<Window x:Class="DOCS_APP_ELEMENT.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:usercontrol="clr-namespace:DOCS_APP_ELEMENT"
    xmlns:viewModel="clr-namespace:DOCS_APP_ELEMENT.ViewModels"
    Title="MainWindow" Height="350" Width="400">
<Grid Margin="10" Name="myGrid">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition Height="Auto"></RowDefinition>
    </Grid.RowDefinitions>
    <Border CornerRadius="5" BorderBrush="SteelBlue" BorderThickness="2" Grid.Row="0">
        <StackPanel Orientation="Vertical">
            <StackPanel Orientation="Horizontal">
                <Label Content="Type:" Margin="20,0,4,0"></Label>
                <ComboBox Name="cmbQuestionType" Width="300" Style="{Binding ComboBoxStyle}" Margin="0,5,0,5" IsEnabled="False">                   </ComboBox>
            </StackPanel>
            <StackPanel Orientation="Horizontal" Margin="0,5,0,5">
                <Label Content="Question:" Margin="0,0,4,0"></Label>
                <TextBox Name="txtQuestion" Width="300" Height="50" Margin="0,2,0,0" AcceptsReturn="True"></TextBox>
            </StackPanel>
            <StackPanel Orientation="Horizontal" Margin="0,5,0,5" >
                <Label Content="Answer:" Margin="7,0,4,0"></Label>
                <TextBox Name="txtAnswer" Style="{StaticResource TextboxStyle}" Margin="0,2,0,0"></TextBox>
            </StackPanel>
        </StackPanel>
    </Border>
    <Border CornerRadius="5" BorderBrush="SteelBlue" BorderThickness="2" Grid.Row="1" Margin="0,10,0,0" >
        <ScrollViewer VerticalScrollBarVisibility="Auto" Height="100">
        <StackPanel Name="myCustom" Orientation="Vertical" >
                **<!--<ADD CUSTOM CONTROl HERE>-->**
            </StackPanel>
        </ScrollViewer>
    </Border>
    <Border CornerRadius="5" BorderBrush="SteelBlue" BorderThickness="2" Grid.Row="2" Margin="0,10,0,0">
        <Border.DataContext>
            <viewModel:ViewElements/>                    
        </Border.DataContext>
        <Button  Name="btnAdd" Content="Add" DataContext="{Binding }" Command="{Binding Path=AddInstace}"></Button>
    </Border>
</Grid>

  • 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-28T03:51:41+00:00Added an answer on May 28, 2026 at 3:51 am

    I’d do it the following way:

    have a ObservableCollection<CustomClass> in your ViewModel. The representation of your CustomClass is a DataTemplate with your above Markup.

    Here’s a full working example:

      <Grid>
        <Grid.DataContext>
          <local:MyViewModel></local:MyViewModel>
        </Grid.DataContext>
          <StackPanel>
          <ScrollViewer VerticalScrollBarVisibility="Auto" Height="200">
            <ItemsControl ItemsSource="{Binding CustomControls}">
              <ItemsControl.ItemTemplate>
                <DataTemplate>
                  <Border Background="Green"> 
                    <StackPanel>
                      <TextBlock Text="I am a Custom Control"></TextBlock>
                      <TextBlock Text="{Binding DisplayValue}"></TextBlock>
                    </StackPanel>
                  </Border>
                </DataTemplate>
              </ItemsControl.ItemTemplate>
              <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                  <StackPanel/>
                </ItemsPanelTemplate>
              </ItemsControl.ItemsPanel>
              </ItemsControl>
          </ScrollViewer>
          <Button Width="200" Height="50" Command="{Binding AddControlCommand}">Add Control</Button>
          <Button Width="200" Height="50" Command="{Binding RemoveControlCommand}">Remove Control</Button>
        </StackPanel>
      </Grid>
    

    ViewModel:

      public abstract class ViewModel : INotifyPropertyChanged
      {
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected void OnPropertyChanged(string propertyName)
        {
          if (PropertyChanged != null)
          {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
          }
        }
      }
    
      public class RelayCommand : ICommand
      {
        ... look that up yourself if you don't have a derived command class yet in your project... 
      }
    
      public class MyViewModel : ViewModel
      {
        public ICommand AddControlCommand
        {
          get
          {
            return new RelayCommand(x=>this.AddControl());
          }
        }
    
        public ICommand RemoveControlCommand
        {
          get
          {
            return new RelayCommand(x => this.RemoveControl());
          }
        }
    
        private void AddControl()
        {
          CustomControls.Add(new CustomControl() {DisplayValue = "newControl"});
        }
    
        private void RemoveControl()
        {
          if (CustomControls.Count > 0)
          {
            CustomControls.Remove(CustomControls.Last());
          }
        }
    
        private ObservableCollection<CustomControl> _customControls;
    
        public ObservableCollection<CustomControl> CustomControls
        {
          get
          {
            if (_customControls == null)
            {
            _customControls = new ObservableCollection<CustomControl>()
                     {
                       new CustomControl() {DisplayValue = "Control1"},
                       new CustomControl() {DisplayValue = "Control2"},
                       new CustomControl() {DisplayValue = "Control3"}
                     };
            }
            return _customControls;
          }
        }
      }
    
      public class CustomControl : ViewModel
      {
        public string DisplayValue { get; set; }
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WPF Application using MVVM pattern. I have a Window in my
I have a custom control that inherits from .NET's CompositeControl class. This control overrides
I have a custom control that can be added multiple times to a form.
I am learning JQuery. I have a need to create a custom control. This
I need to implement a Ruler and I have a CustomControl RangeSlider which is
I'm using a the WPF datagrid from the Microsoft CodePlex project. I have a
Aloha, I have a custom control that I need to instantiate from within a
Hello I have a custom control. This custom control has it's DataContext set to
I have custom control derived from Panel and I need to handle selecting with
I have an ASP.NET GridView which has columns that look like this: | Foo

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.