2011-03-31 16 views
0

嗨,我有這樣的C#的tabcontrol和電網問題

<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    x:Class="test.Window1" 
    x:Name="Window" 
    Title="Window1" 
    Width="640" Height="480"> 

    <Grid x:Name="LayoutRoot"> 
     <Button Content="Create a tab" HorizontalAlignment="Left" Margin="49,26,0,0" VerticalAlignment="Top" Width="75"/> 
     <TabControl Margin="0,63,0,0"> 

     </TabControl> 
    </Grid> 
</Window> 
中的TabControl

沒有TabItem的有一個XAML代碼。 請幫助我,如何用C#編程: 如果我點擊按鈕,它會添加一個帶有網格和文本塊的選項卡。結果我希望這樣:

<Grid x:Name="LayoutRoot"> 
    <Button Content="Create a tab" HorizontalAlignment="Left" Margin="49,26,0,0" VerticalAlignment="Top" Width="75"/> 
    <TabControl Margin="0,63,0,0"> 
     <TabItem Header="tab1"> 
      <Grid> 
       <TextBlock Text="hi there" /> 
      </Grid> 
     </TabItem> 
    </TabControl> 
</Grid> 

如果我點擊更多的按鈕,將繼續添加這樣的標籤。

請幫我(崇拜)

回答

3

鑑於這是您的XAML:

<Grid x:Name="LayoutRoot"> 
    <Button Content="Create a tab" HorizontalAlignment="Left" Margin="49,26,0,0" VerticalAlignment="Top" Width="75"/> 
    <TabControl Margin="0,63,0,0" x:Name="MyTabControl"> 
     <TabItem Header="tab1"> 
      <Grid> 
       <TextBlock Text="hi there" /> 
      </Grid> 
     </TabItem> 
    </TabControl> 
</Grid> 

您可以在代碼隱藏添加的TabItem像這樣:

TextBlock t = new TextBlock { Text= "hi" }; 
Grid g = new Grid; 
g.Children.Add(t); 
TabItem t = new TabItem(); 
t.Content = g; 
MyTabControl.Children.Add(t);