1.通过代码形式, 先加入Panel, 然后依次加入你要的控件。
StackPanel panel = new StackPanel();
Button button=new Button(){Content="button1"};
TextBlock textblock= new TextBlock(){Text="TextBlock1"};
panel.Children.Add(button);
panel.Children.Add(textblock);
TabItem item= tabcontrol.Items[0] as TabItem;
item.Content = panel;
2.通过MVVM绑定形式,你只要在VM中动态增加你的成员,前面View就会动态显示出TabItem及内容:
public partial class MainWindow : Window {
public MainWindow()
{
InitializeComponent();
this.DataContext = new ObservableCollection<object>{ new Item1(), new Item2(), }; }
}
public class Item1 { }
public class Item2 { }
XMAL:
<Window x:Class="WpfApplication7.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication7"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<DataTemplate DataType="{x:Type local:Item1}">
<Button Content="Button"/>
</DataTemplate>
<DataTemplate DataType="{x:Type local:Item2}">
<TextBox Text="Button"/>
</DataTemplate>
</Window.Resources>
<Grid>
<TabControl ItemsSource="{Binding}">
<TabControl.ItemContainerStyle>
<Style TargetType="{x:Type TabItem}">
<Setter Property="Header" Value="Header"/>
<Setter Property="ContentTemplate" Value="{Binding}"/>
</Style>
</TabControl.ItemContainerStyle>
</TabControl>
</Grid>
</Window>