2011-06-11 35 views
1

在Silver light Tab控件中,我使用xaml添加了自定義選項卡標題(名稱和關閉按鈕)完美地工作。在.cs文件中使用silverlight的自定義Tabcontrol? (在自定義tabitem標題內添加堆棧面板不起作用)

{XAML代碼}

<Grid Height="559" Name="grid1" Width="953"> 
<sdk:TabControl Height="391" HorizontalAlignment="Left" Margin="105,57,0,0" Name="tabControl1" VerticalAlignment="Top" Width="729"> 
    <sdk:TabItem Name="tabItem1" IsTabStop="False"> 
     <sdk:TabItem.Header> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock Text="New Tab" Margin="1,1,1,1" VerticalAlignment="Center" /> 
       <Button Content="X" /> 
      </StackPanel> 
     </sdk:TabItem.Header> 
     <Grid /> 
    </sdk:TabItem> 
</sdk:TabControl> 
<Button Content="+" Height="23" HorizontalAlignment="Left" Margin="74,57,0,0" Name="button1" VerticalAlignment="Top" Width="31" Click="button1_Click" /> 
<Button Content="-" Height="23" HorizontalAlignment="Left" Margin="12,492,0,0" Name="button2" VerticalAlignment="Top" Width="31" Click="button2_Click" Visibility="Collapsed" /> 

相同的實現試過的.cs但我可以添加堆疊面板的新標籤頭

代碼中,供您參考

StackPanel st = new StackPanel(); 
      st.Orientation = Orientation.Horizontal; 
      TextBlock txtb = new TextBlock(); 
      txtb.Text = "test"; 
      txtb.Margin = new Thickness(1, 1, 1, 1); 
      txtb.VerticalAlignment = VerticalAlignment.Center; 
      st.Children.Add(txtb); 
      Button btn = new Button(); 
      btn.Content = "X";   
      st.Children.Add(btn);   

      tabControl1.Items.Add(new TabItem 
      { 
       Header =st    

      }); 

幫我解決這個問題。我需要自定義選項卡標題與按鈕控制

回答

2

您應該設置tbItem.Header = st。 tbItem.Content用於定義選項卡的內容,而不是選項卡標題。

您的代碼將是這個樣子

StackPanel st = new StackPanel(); 
    st.Orientation = Orientation.Horizontal; 
    TextBlock txtb = new TextBlock(); 
    txtb.Text = "New Tab"; 
    txtb.Margin = new Thickness(1, 1, 1, 1); 
    txtb.VerticalAlignment = VerticalAlignment.Center; 
    st.Children.Add(txtb); 
    Button btn = new Button(); 
    btn.Content = "X";  
    st.Children.Add(btn); 

    TabItem tbitem = new TabItem(); 
    // Set the header to the stack panel with the 
    // TextBlock and Button 
    tbitem.Header = st; 

    // This is where you define the content 
    // of the tab page. Here I just added a Grid 
    // as an example. 
    tbitem.Content = new Grid(); 

    tabControl1.Items.Add(tbitem); 

編輯:這是一個完整的例子

XAML隨着TabControl的 - 請注意,我勾Loaded事件,這是我將添加動態的TabItem 。

<UserControl x:Class="SilverlightApplication1.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" 
    mc:Ignorable="d" 
    d:DesignHeight="300" d:DesignWidth="400" > 
    <Grid x:Name="LayoutRoot" Background="White"> 
    <Grid x:Name="Container">  
     <controls:TabControl Name="tabControl1" Loaded="TabControl_Loaded"> 

     </controls:TabControl> 
    </Grid> 
    </Grid> 
</UserControl> 

這裏是背後

using System; 
using System.Windows; 
using System.Windows.Controls; 

namespace SilverlightApplication1 
{ 
    public partial class MainPage : UserControl 
    { 
    public MainPage() 
    { 
     InitializeComponent(); 
    } 

    private void TabControl_Loaded(object sender, RoutedEventArgs e) 
    { 
     StackPanel st = new StackPanel(); 
     st.Orientation = Orientation.Horizontal; 
     TextBlock txtb = new TextBlock(); 
     txtb.Text = "New Tab"; 
     txtb.Margin = new Thickness(1, 1, 1, 1); 
     txtb.VerticalAlignment = VerticalAlignment.Center; 
     st.Children.Add(txtb); 
     Button btn = new Button(); 
     btn.Content = "X";  
     st.Children.Add(btn); 

     TabItem tbitem = new TabItem(); 
     // Set the header to the stack panel with the 
     // TextBlock and Button 
     tbitem.Header = st; 

     // This is where you define the content 
     // of the tab page. Here I just added a Grid 
     // as an example. 
     tbitem.Content = new Grid(); 

     tabControl1.Items.Add(tbitem); 
    } 
    } 
} 
+0

@克里斯·泰勒代碼:感謝您的回覆。我想在TabItem標題內添加stackpanel而不是tabitem內容。 – Ash 2011-06-11 13:12:28

+0

@Ash,這就是'tbitem.Header = st'的作用,它將StackPanel分配給TabItem頭,這正是您的XAML所做的。 – 2011-06-11 13:15:13

+0

是否有可能在tabitem中添加自定義標題的stackpanel? – 2011-06-11 13:15:14

相關問題