Nothing special.
Your combo box controls the content placed in an instance of a TabItem, correct?
First, Why
ComboBox
, not
ListBox
? Think about it. No matter, they both will work the same way.
What do you want to show in
ComboBox
item? Let's assume, it should be plain test item. At the same time, you need some extra information on each item which will instruct how to populate a
TabItem
instance. So real content of the combo box item should be some information type which can behave like string. The way to achieve this is creating some information type which also returns the name of the item via overridden
ToString
. It will look something like that:
struct ItemInfo {
internal ItemInfo(string name ) { Name = name; }
public override string ToString() {
return Name.ToString();
}
string Name;
}
Now, you need to populate your controlling
ComboBox
with items of the type
ItemInfo
:
string name =
ItemInfo info = new ItemInfo(name);
box.Items.Add(info);
Setup event handler of the event
SelectionChanged
of your controlling
ComboBox
instance:
myComboBox.SelectionChanged += (sender, args) => {
ItemInfo info;
System.Collections.IList itemList = args.AddedItems;
if (itemList == null || itemList.Count < 1)
info = default(ItemInfo);
else
info = (ItemInfo)args.AddedItems[0];
PopulateTabItem(MyTabItem item, info);
};
Population of the Tab item can be something like this:
void PopulateTabItem(TabItem item, ItemInfo info) {
DockPanel panel = new DockPanel();
item.Content = panel;
TextBox myTextBox = new TextBox();
ListBox myListBox = new ListBox();
ComboBox myComboBox = new ComboBox();
panel.Children.Add(myTextBox);
panel.Children.Add(myComboBox);
panel.Children.Add(myListBox);
}
Now, you really need to think, do you
really need fully dynamic population? I suspect, you may need just a few predefined combinations of different child controls in the content of you dynamic
TabItem
.
In this case, create each combination as a separate
UserControl
. Your dynamic population will simply assign your tab item's
Content
to the
UserControl
one or another type. Also, with
UserControl
you will be able to easily encapsulate data exchange between you dynamic controls and data at the level of the form (or whatever on top).
For the last note: your description looks over-designed to complete confusion of your user. First, you mention Main tab first, then somewhere inside "ChildTabControl with 5 tabs". Do you want to make a tab control inside a tab control? I don't recommend, this will confuse the user. On top of that, you want to put dynamic population not next to the controlling combo box, but inside one of 5 tab items. But the dynamic content of the tab item may or may not be visible when you operate your controlling combo box (not visible when some other tab is selected). This will be even more confusing. Make it strict and simple.
The idea of controlling dynamic content by a combo box is not bad (but, perhaps, list box, again, what do you want to to with combo?!), but make it regular. For example, on every top of hierarchy there are Tabs. On some of the tabs -- lower level -- there are controlling boxes on left with controlled dynamic contents on right, or fixed content in a tab item. And not more complex than that. If you want deeper nesting, forget your boxes and tabs, use
TreeView
, after all. But if you do it more complex than I describe or use inconsistent way of presentation of hierarchy, you will drive your user crazy!
Take care.
Thank you.