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.
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:
And now your XAML Code:
And last but not least MyModel:
If you need the example Project contact me.