Click here to Skip to main content
16,015,047 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi,

I have two user controls in one Main tab. (Using WPF and C#)
-> first user control consists of one Combo Box and some other elements like TextBoxes.
-> Second user Controls consists of ChildTabControl with 5 tabs.

the content inside the second childTabItem is dynamic (Which will generate textbox, combobox, listbox, etc.) based on selected item in combobox(this is in first user control).

Please suggest me or share your knoweledge to complete this

Regards
Ramesh
Posted
Updated 16-Dec-10 21:45pm
v2
Comments
Sergey Alexandrovich Kryukov 1-Mar-11 3:04am    
OP commented:

Hi

Thanks for your reply and valuable suggestion with brief description. According to the customer requirement, I need child tabcontrol inside the Main tabcontrol.
I completed this task by taking one seperate class which will generate Dynamic controls by taking the Id from the ComboBox and then these dynamic controls will be passed to the second usercontrol.

1 solution

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:

C#
struct ItemInfo {
    internal ItemInfo(string name /* something else ... */) { Name = name; }
    public override string ToString() {
        return Name.ToString();
    } //ToString
    string Name;
    //whatever else
    //...
} //struct ItemInfo


Now, you need to populate your controlling ComboBox with items of the type ItemInfo:

C#
//...
string name = //...
ItemInfo info = new ItemInfo(name);
box.Items.Add(info);
//...


Setup event handler of the event SelectionChanged of your controlling ComboBox instance:

C#
myComboBox.SelectionChanged += (sender, args) => {
    ItemInfo info;
    System.Collections.IList itemList = args.AddedItems;
    if (itemList == null || itemList.Count < 1)
        info = default(ItemInfo); //to guard agains the case
        //when nothing is selected
    else
        info = (ItemInfo)args.AddedItems[0];
    PopulateTabItem(MyTabItem item, info);
}; //box.SelectionChanged


Population of the Tab item can be something like this:

C#
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);
    //adjust layout...
    //...    
} //PopulateTabItem


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.
 
Share this answer
 
v3
Comments
Espen Harlinn 26-Feb-11 10:56am    
Good reply - a 5
Sergey Alexandrovich Kryukov 26-Feb-11 20:10pm    
Wow! nobody replied to this one. Thank you for reminding me and your vote! I'll probably reuse it, too.
--SA
_Ashish 27-Feb-11 6:25am    
i liked thw way its defined +5
Sergey Alexandrovich Kryukov 1-Mar-11 3:03am    
Thank you, Ashish,
--SA
Sergey Alexandrovich Kryukov 1-Mar-11 3:04am    
Ramesh, thanks for accepting my Answer.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900