如何根据组合选择的值填充选项卡项目的内容 [英] How to populate Tab item content based on combo selected value

查看:79
本文介绍了如何根据组合选择的值填充选项卡项目的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在一个主要"标签中有两个用户控件. (使用WPF和C#)
->第一个用户控件由一个组合框和其他一些元素(如文本框)组成.
->第二个用户控件由带有5个选项卡的ChildTabControl组成.

第二个childTabItem内部的内容是动态的(这将根据组合框中的选定项(将在第一个用户控件中)生成文本框,组合框,列表框等).

请建议我或分享您的知识以完成此操作

问候
Ramesh

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

推荐答案

没什么特别的.
您的组合框控制放置在TabItem实例中的内容,对吗?
首先,为什么是ComboBox,而不是ListBox?想一想.没关系,它们都将以相同的方式工作.

您想在ComboBox项目中显示什么?假设它应该是简单的测试项目.同时,您需要有关每个项目的一些额外信息,这些信息将指示如何填充TabItem实例.因此,组合框项目的真实内容应该是某种信息类型,其行为类似于字符串.实现此目的的方法是创建一些信息类型,该信息类型还将通过覆盖的ToString返回项的名称.看起来像这样:

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 /* something else ... */) { Name = name; }
    public override string ToString() {
        return Name.ToString();
    } //ToString
    string Name;
    //whatever else
    //...
} //struct ItemInfo



现在,您需要使用ItemInfo类型的项目填充控制ComboBox:



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);
//...



设置控制ComboBox实例的事件SelectionChanged的事件处理程序:



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); //to guard agains the case
        //when nothing is selected
    else
        info = (ItemInfo)args.AddedItems[0];
    PopulateTabItem(MyTabItem item, info);
}; //box.SelectionChanged



Tab项的填充可以是这样的:



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



现在,您真的需要考虑,您是否真的需要完全动态的人口?我怀疑,在动态TabItem的内容中,您可能只需要几个不同子控件的预定义组合.
在这种情况下,请将每个组合创建为单独的UserControl.您的动态填充只会将选项卡项的Content分配给UserControl的一种或另一种类型.此外,使用UserControl,您将能够轻松地封装动态控件和数据在表单级别(或最上面的任何级别)之间的数据交换.

最后一点:您的说明看起来过于设计,以至于使您的用户完全困惑.首先,首先提到主要"选项卡,然后在带有5个选项卡的ChildTabControl"中的某个位置.是否要在选项卡控件内部制作选项卡控件?我不建议这样做,这会使用户感到困惑.最重要的是,您不希望将动态填充放置在控制组合框的旁边,而是放置在5个选项卡项之一的内部.但是,当您操作控制组合框时,选项卡项目的动态内容可能会或可能不可见(选择其他某个选项卡时不可见).这将更加令人困惑.使其变得严格而简单.
通过组合框控制动态内容的想法不错(但是,也许列表框还是要对组合使用什么?!),但是要使其规则化.例如,在层次结构的每个顶层都有选项卡.在某些选项卡(较低级别)上,左侧具有控制框,右侧具有受控的动态内容,或者选项卡项中具有固定的内容.而且没有比这更复杂的了.如果您想进行更深层的嵌套,请忘了您的框和选项卡,毕竟请使用TreeView.但是,如果您执行的操作比我描述的复杂,或者使用不一致的层次结构表示方式,则会使您的用户发疯!

保重.
谢谢.



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.


这篇关于如何根据组合选择的值填充选项卡项目的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆