Reputation: 477
In Silver light Tab control ,i have adding custom tab header (name along with close button) using xaml Working perfectly .
{xaml code}
<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" />
Same implementation tried with .cs but i Could add stack panel inside the new tab header
code for your reference
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
});
Help me to solve this problem . i need Custom tab header With button control
Upvotes: 1
Views: 1437
Reputation: 53699
You should be setting tbItem.Header = st
. tbItem.Content is used to define the content of the tab, not the tab header.
Your code would look something like this
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);
Edit: Here is a complete example
XAML With TabControl - Notice that I hook the Loaded event, this is where I will add the dynamic 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>
Here is the Code behind
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);
}
}
}
Upvotes: 2