0

I have a xaml which contains a tab control (Name="MyTabControl"). I'm a beginner with wpf and in code and I want to dynamically add tab items that I then add a list box to each created tab item. Below is the code I have so far.

ListBox listbox = new ListBox()
TabItem tab = new TabItem()
tab.AddChild(listbox)

MyTabControl.Add(tab)

My issue is that I can't figure out how dynamically create new tabs that also would add a list box to each new tab and the new tabs then added to MyTabControl. Then, I would want to be able to access each list box control, individually, in each tab to edit the list box content.

How is this done in code? How can i access the created list box controls to edit them?

rr0711
  • 17
  • 8
  • Thanks for the responses. Looks like I need to research and learn up on MVVM in order to do this. – rr0711 Mar 14 '18 at 19:19

2 Answers2

0

WPF/UWP and XAML are designed with the MVVM pattern in mind. While you can use other approaches, doing so will miss about 90% of it's power and run into issues at every other corner.

In MVVM this would be simply a mater of Exposing a Collection and having a Tempalte targetting that type. ListBoxes might even have a custom Template system, but using ListBoxes might no longer be nessesary - any container can expose a Collection.

If you plan on learning MVVM, mid to longertem you should learn MVVM. I wrote a short intro a few years back, that should help you going. Help for people not following MVVM is parse on the Forum.

Christopher
  • 9,634
  • 2
  • 17
  • 31
0

In general, it's a violation of the MVVM principles WPF is built around to build a UI in this way. Instead, consider a solution similar to the one proposed in the answers to this question. They do a good job of explaining both what to do, and why we do it this way.

Zarenor
  • 1,161
  • 1
  • 11
  • 23