2012-05-15 45 views
0

以下代碼創建2個按鈕點擊的選項卡,但不顯示任何內容。我仍然在學習這一點,並想知道錯誤是什麼。使用不同的內容動態創建2個選項卡wpf

背後

public void button2_Click(object sender, RoutedEventArgs e) 
{ 
    popUp1.IsOpen = false; 

    Canvas canvas4 = new Canvas(); 
    ScrollViewer viewer4 = new ScrollViewer(); 
    viewer4.Content = canvas4; 

    string txtfl = File.ReadAllText(textBox1.Text); 
    TextBlock txbl1 = new TextBlock(); 
    txbl1.Text = txtfl; 
    canvas4.Children.Add(txbl1); 

    TabItem newTab1 = new TabItem(); 
    newTab1.Header = "Text File"; 
    newTab1.Content = viewer4; 
    tabctrl1.Items.Add(newTab1); 

    tabctrl1.SelectedItem = newTab1; 

    Brush br = new SolidColorBrush(Colors.White); 
    Canvas canvas2 = new Canvas(); 
    canvas2.Background = br; 
    ScrollViewer viewer2 = new ScrollViewer(); 
    viewer2.Content = canvas2; 

    TabItem newTab2 = new TabItem(); 
    newTab2.Header = "Test"; 
    newTab2.Content = viewer2; 
    tabctrl1.Items.Add(newTab2); 
} 

TabControl的代碼使用XAML

XAML

<Window ... > 
    <Grid> 
     <Popup Name="popUp1" Width="500" Height="300" Placement="Center" IsEnabled="False" IsOpen="False"> 
      <Canvas x:Name="myCanvas1" Width="500" Height="300" Visibility="Visible"> 
       <Button Content="Button" Height="38" HorizontalAlignment="Left" Margin="12,71,0,0" Name="button1" VerticalAlignment="Top" Width="151" Click="button2_Click" />        
      </Canvas> 
     </Popup> 

     <TabControl Name="tabctrl1" Width="1345" Height="28" Margin="0,24,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" BorderThickness="2"> 
      <TabControl.Background> 
       <LinearGradientBrush EndPoint="0,1" StartPoint="0,0"> 
        <GradientStop Color="#FF3A60AD" Offset="0.528"/> 
        <GradientStop Color="#FF6A85D8" Offset="0.01"/> 
       </LinearGradientBrush> 
      </TabControl.Background>     
     </TabControl> 
    </Grid> 
</Window> 
+1

提供畫布的高度和寬度。然後看看會發生什麼。 –

+1

對XAML元素進行編碼時,通常最好先用圖形編輯器進行編輯,然後查看需要包含在元素中的屬性。 –

+1

如果你正在學習WPF,我會建議你完全忘記你之前有關構建UI和使用適當方式的知識。您正在創建代碼隱藏的所有內容,您最好使用XAML。如果你發佈你的tabctrl1來自哪裏,這將會有所幫助。像Snoop這樣的工具也是學習WPF如何構建和佈局控件的好方法。 – dowhilefor

回答

0

嗨你的代碼似乎沒有工作的原因是因爲選項卡控件的高度設置創建到28.

試着將它改爲280,你應該能夠看內容。

<TabControl Name="tabctrl1" Width="1345" Height="280" Margin="0,24,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" BorderThickness="2"> 
相關問題