在 .cs 文件中使用 silverlight 自定义 Tabcontrol? (在自定义 tabitem 标题中添加堆栈面板不起作用)

发布于 2024-11-15 00:00:08 字数 1763 浏览 2 评论 0原文

在 Silver light Tab 控件中,我使用 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">
    <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              

            });

帮助我解决此问题。我需要带有按钮控件的自定义选项卡标题

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫轩蝶泪 2024-11-22 00:00:08

您应该设置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);

编辑:这是一个带有 TabControl 的完整示例

XAML - 请注意,我挂钩了 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);
    }
  }
}

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);
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文