xaml:
<Grid x:Name="LayoutRoot" Background="White">
<sdk:TabControl Height="100" HorizontalAlignment="Left" Margin="72,136,0,0" Name="tabControl1" VerticalAlignment="Top" Width="200">
<sdk:TabItem Header="tabItem1" Name="tabItem1">
<Grid />
</sdk:TabItem>
</sdk:TabControl>
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="118,44,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
</Grid>
code behind:
public MainPage()
{
InitializeComponent();
TabItem ti = new TabItem();
tabControl1.Items.Add(ti);
Grid g1 = new Grid();
g1.Name = "g1";
ti.Content = g1;
Grid g2 = new Grid();
g2.Name = "g2";
g1.Children.Add(g2);
object o1 = FindName("g1");
object o2 = FindName("g2");
}
private void button1_Click(object sender, RoutedEventArgs e)
{
TabItem ti = new TabItem();
tabControl1.Items.Add(ti);
Grid g3 = new Grid();
g3.Name = "g3";
ti.Content = g3;
Grid g4 = new Grid();
g4.Name = "g4";
g3.Children.Add(g4);
object o1 = FindName("g3");
object o2 = FindName("g4");
}
最后一个g4始终找不到,不知为何,本人初学silverlight,很多不懂,希望高手们能不吝赐教
我来赐教啦~
把tabControl1.Items.Add(ti);
移动到g3.Children.Add(g4);
下面,原因还没研究