我会这样做(代码用于 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>
主窗口.xaml.cs
public MainWindow()
{
InitializeComponent();
var tab = new MyTabItem {MyContent = "Content", MyTitle = "Title"};
MyTabControl.Items.Add(tab);
}
主窗口.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>