C# 在.cs文件中使用silverlight的自定义Tabcontrol?(在自定义tabitem标题中添加堆栈面板无效)
在Silverlight选项卡控件中,我使用xaml添加了自定义选项卡标题(名称和关闭按钮),效果非常好 {xaml代码}C# 在.cs文件中使用silverlight的自定义Tabcontrol?(在自定义tabitem标题中添加堆栈面板无效),c#,visual-studio-2010,silverlight-4.0,tabcontrol,C#,Visual Studio 2010,Silverlight 4.0,Tabcontrol,在Silverlight选项卡控件中,我使用xaml添加了自定义选项卡标题(名称和关闭按钮),效果非常好 {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">
<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" />
帮我解决这个问题。我需要带有按钮控件的自定义选项卡标题您应该设置
tbItem.header=st
。内容用于定义选项卡的内容,而不是选项卡标题
您的代码看起来像这样
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—注意,我钩住了加载的事件,这是我将添加动态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>
@克里斯·泰勒:谢谢你的回复。我想在TabItem标题中添加stackpanel,而不是TabItem内容。@Ash,这就是
tbitem.header=st
所做的,它将stackpanel分配给TabItem标题,这正是XAML所做的。是否可以在TabItem中为自定义标题添加stackpanel?@Asta ni enohpi,是的,通过将StackPanel实例分配给TabItem.Header,标题将承载StackPanel。@Chris taylor:请参见我编辑的问题。但该按钮仍然不可见,选项卡的名称也显示为“新选项卡”,而不是“测试”。背后的意思是,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);
}
}
}