TabControl在每个选项卡上具有相同的控件

时间:2012-08-26 11:30:27

标签: c# winforms .net-3.5 tabcontrol

我想在TabControl的所有页面上使用相同的控件。

我的tabControl表格中没有任何tabPages。在相同的表单中,我有一些控件,我想自动添加到每个tabPage。并与他们合作。我怎么能这样做?

我想要这样的事情:

    public chatForm(string chatID, object conversation)
    {
        InitializeComponent();

        createNewTab(chatID, conversation);
    }

    internal void createNewTab(string chatID, object conversation)
    {
        ISIMtabPage newTab = new ISIMtabPage();
        newTab.Text = Converter.getContactName(conversation);
        newTab.Name = chatID;

        newTab.conversation = conversation;

        newTab.avatarBox.Image = new Bitmap("graphics\\anonymousAvatar.png");// how can I work with this control in the selected tab?

我怎样才能在每个tabPage中自动执行所有控件,以及如何使用它们?

2 个答案:

答案 0 :(得分:1)

我会这样做(代码是针对WPF解决方案,已经过度使用了WinForm-Tag。但是应该类似。):

MyTabItem.xaml.cs

public partial class MyTabItem : TabItem, INotifyPropertyChanged
{
    #region PropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    void OnPropertyChanged(string propName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }

    #endregion

    #region Properties

    private string myTitle;
    public string MyTitle
    {
        get { return myTitle; } 
        set 
        {
            myTitle = value;
            OnPropertyChanged("MyTitle");
        }
    }

    private string myContent;
    public string MyContent { 
        get { return myContent; } 
        set 
        { 
            myContent = value;
            OnPropertyChanged("MyContent");
        } 
    }

    #endregion

    public MyTabItem()
    {
        InitializeComponent();
    }
}

MyTabItem.xaml

<TabItem x:Class="WpfApplication1.MyTabItem"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            DataContext="{Binding RelativeSource={RelativeSource self}}">

    <TabItem.Header>
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding MyTitle}"/>
        </StackPanel>
    </TabItem.Header>

    <TabItem.Content>
        <Grid>
            <TextBlock Text="{Binding MyContent}" Background="Azure"/>
        </Grid>
    </TabItem.Content>
</TabItem>

MainWindow.xaml.cs

public MainWindow()
{
    InitializeComponent();

    var tab = new MyTabItem {MyContent = "Content", MyTitle = "Title"};

    MyTabControl.Items.Add(tab);
}

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
        <TabControl x:Name="MyTabControl"/>
    </Grid>
</Window>

答案 1 :(得分:1)

  

我有自己的类,它扩展了TabPage组件。我只是添加我的属性。但我想在所有页面上都有相同的组件。如果我调用addMessage方法,它只需将文本添加到tabPage [id] .conversationTextBox.Append(message); [...]如果我点击另一个标签,它将显示其他当前对话。我不明白,也不知道该怎么做。 - sczdavos

这是因为原始类型将通过值传递,通过引用复杂! 我找到了关于参考和值类型的这个例子,希望你能从中学到[1]。阅读完之后试试这个:

  

@sczdavos通常您创建一个包含您要使用的所有属性的类,并在视图中共享该实例。 [...]

[1] http://www.codeproject.com/Articles/32029/Reference-and-Value-Types-in-C