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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:48:16+00:00 2026-06-17T08:48:16+00:00

this is my code to dynamically add tabitem to tabcontrol: TabItem newTab = new

  • 0

this is my code to dynamically add tabitem to tabcontrol:

TabItem newTab = new TabItem();
newTab.Header = ncn.courseName;
newTab.FontSize = 20;
TextBox textbox = new TextBox();
textbox.HorizontalAlignment = HorizontalAlignment.Stretch;
textbox.VerticalAlignment = VerticalAlignment.Stretch;
textbox.FontSize = 12;
textbox.AcceptsReturn = true;
newTab.Content = textbox;
this.Courses.Items.Add(newTab);
this.Courses.SelectedItem = newTab;

I think there might be better way to do this (ie, define the UI in xaml). I’m new to WPF and couldn’t figure out how the template thing works. So please help me!

NOTE: the tabcontrol is empty at the beginning (showing nothing, no tabitem, no textbox)! I want to add new tab only when I click the “add” button.


Someone helped me and figured it out.

  • 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-17T08:48:16+00:00Added an answer on June 17, 2026 at 8:48 am

    The first thing is: It is in 99% of all cases the best and possible to write you UI in XAML.

    Second you should inform yourself, whats the WPF indicates and how to use the MVVM. If you don’t you should stay by Windows Forms!

    So now:

    You ViewModel:

    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using PB.Base;
    
    namespace DynTabItems
    {
        public class MainWindowViewModel
        {
            public MainWindowViewModel()
            {
                AddItemCommand = new DelegateCommand(AddItem, CanAddItem);
                RemoveItemCommand = new DelegateCommand(RemoveItem, CanRemoveItem);
            }
    
            public DelegateCommand AddItemCommand { private set; get; }
            public DelegateCommand RemoveItemCommand { private set; get; }
    
            ObservableCollection<MyModel> _yourBehaviorClass = new ObservableCollection<MyModel>();
            MyModel _selectetItem;
    
            public MyModel SelectetItem
            {
                get { return _selectetItem; }
                set { _selectetItem = value; }
            }
    
            public ObservableCollection<MyModel> YourBehaviorClass
            {
                get { return _yourBehaviorClass; }
                set { _yourBehaviorClass = value; }
            }
    
            private void AddItem(object obj)
            {
                YourBehaviorClass.Add(new MyModel());
            }
    
            private bool CanRemoveItem(object obj)
            {
                return SelectetItem != null;
            }
    
            private bool CanAddItem(object obj)
            {
                return true;
            }
    
            private void RemoveItem(object obj)
            {
                YourBehaviorClass.Remove(SelectetItem);
            }
        }
    }
    

    And now your XAML Code:

    <Window x:Class="DynTabItems.MainWindow"
                    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    Title="MainWindow" Height="350" Width="525">
        <StackPanel>
            <Button Content="Add" Command="{Binding AddItemCommand}"/>
            <Button Content="Remove" Command="{Binding RemoveItemCommand}"/>
            <TabControl ItemsSource="{Binding YourBehaviorClass}" SelectedItem="{Binding SelectetItem}">
                <TabControl.ItemContainerStyle>
                    <Style TargetType="TabItem">
                        <Setter Property="Header" Value="{Binding YourHeader, UpdateSourceTrigger=PropertyChanged}"/>
                        <Setter Property="Content">
                            <Setter.Value>
                                <StackPanel>
                                    <TextBox Text="{Binding YourText, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}">
                                        <ToolTipService.ToolTip>
                                            <TextBlock Text="{Binding Error}"/>
                                        </ToolTipService.ToolTip>
                                    </TextBox>
                                </StackPanel>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </TabControl.ItemContainerStyle> 
            </TabControl>
        </StackPanel>
    </Window>
    

    And last but not least MyModel:

    using PB.Base;
    using PB.Interfaces;
    using PB.ValidationError;
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DynTabItems
    {
        public class MyModel : ViewModelBaseEx<MyModel>
        {
            public MyModel()
            {
                YourHeader = "You header String";
                ListOfExistingErrors = new ObservableCollection<IValidationErrorInfo<MyModel>>();
                ListOfExistingErrors.Add(new Error<MyModel>() { ErrorIndicator = "YourText", ErrorText = "Your Error Text", Predicate = s => string.IsNullOrEmpty(s.YourText) });
            }
    
            string _yourText;
            string _yourHeader;
    
            public string YourHeader
            {
                get { return _yourHeader; }
                set
                { 
                    _yourHeader = value;
                    SendPropertyChanged(() => YourHeader);
                }
            }
    
            public string YourText
            {
                get { return _yourText; }
                set 
                { 
                    _yourText = value;
                    SendPropertyChanged(() => YourText);
                }
            }
    
            public override ObservableCollection<IValidationErrorInfo<MyModel>> ListOfExistingErrors
            {
                get;
                set;
            }
        }
    }
    

    If you need the example Project contact me.

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

Sidebar

Related Questions

I am having this code which i am using to dynamically add new items
i have this code which add textbox dynamically row_no=0; function addRow(tbl,row){ row_no++; if (row_no<=20){
I need to dynamically create textbox. This is my code, but with this I
This Lua code, creates a table and dynamically adds a new member. Running this
I am using this code to generate the textbox dynamically by using the JavaScript.
so this code here dynamically adds buttons to my wpf windows application. I cant
This code attempts to dynamically switch the class of the StateContainer div from StateOne
i am using this code below to dynamically capture the name of the button
I have this code $(.delete2).click(function() { $('#load2').fadeIn(); } I have dynamically added item via
I need to create a WPF grid dynamically from code behind. This is going

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.