wpf怎样动态绑定TabControl的多个TabItem

wpf怎样动态绑定TabControl的多个TabItem,第1张

1、做法1,重写TabItem的Style,将Width设为一致,前提是这个Width要能兜得住一般的大校 2、做法2,重写TabControl的Style,将它的Panel换成UniformGrid,并设置Rows=1

1.TabControlDemo.xaml是主界面

<UserControl xmlns:sdk="clr-namespace:System.Windows.Controlsassembly=System.Windows.Controls"  x:Class="Silverlight.Common.View.TabControlDemo"

<Grid x:Name="LayoutRoot" Background="White">

        <Grid.RowDefinitions>

            <RowDefinition Height="70" />

            <RowDefinition Height="auto" />

        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="100" />

            <ColumnDefinition Width="auto" />

        </Grid.ColumnDefinitions>

        <StackPanel Grid.Row="1">

<TextBlock Margin="5" x:Name="dataGrid" Text="dataGrid" MouseLeftButtonDown="DataGrid_MouseLeftButtonDown"></TextBlock>

            <TextBlock Margin="5"  x:Name="treeView" Text="treeView" MouseLeftButtonDown="treeView_MouseLeftButtonDown"></TextBlock>

            <TextBlock Margin="5"  x:Name="dataForm" Text="dataForm" MouseLeftButtonDown="dataForm_MouseLeftButtonDown"></TextBlock>

</StackPanel>

        <sdk:TabControl BorderThickness="5" BorderBrush="Green" MinHeight="500" MinWidth="500" Grid.Column="1" Grid.Row="1" x:Name="tabControl">

</sdk:TabControl>

</Grid>

</UserControl>

2.TabControlDemo .cs

using System

using System.Collections.Generic

using System.Linq

using System.Net

using System.Windows

using System.Windows.Controls

using System.Windows.Documents

using System.Windows.Input

using System.Windows.Media

using System.Windows.Media.Animation

using System.Windows.Shapes

namespace Silverlight.Common.View

{

    public partial class TabControlDemo : UserControl

    {

        public TabControlDemo()

        {

            InitializeComponent()

        }

//header是tabItem的标题,typeName是所显示界面的全名(命名空间+类名)

private void AddTabItem(string header, string typeName)

        {

            if (string.IsNullOrEmpty(header) || string.IsNullOrEmpty(typeName))

            {

                return

            }

//获取已经显示的TabItem,将TabItem的header作为检索条件

      var list = this.tabControl.Items.Where(w =>((TabItem)w).Header.ToString() == header)

//如果所点击的窗体已打开,那么显示该窗体,这样避免打开多个同一窗体

if (list.Count() >0)

            {

                ((TabItem)list.First()).Visibility = Visibility.Visible

this.tabControl.SelectedItem = list.First()

            }

            else

            {

                TabItem tabItem = new TabItem()

tabItem.Header = header

//用发射实例化窗体类

                var tabContent = Activator.CreateInstance(Type.GetType(typeName)) as UserControl

tabItem.Content = tabContent

//将实例化的tabItem添加到TabControl

                this.tabControl.Items.Add(tabItem)

                this.tabControl.SelectedItem = tabItem

            }

        }

private void DataGrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)

        {

//Silverlight.Common.View.DataGridDemo是SilverLight学习笔记七DataGrid控件节中的DataGridDemo(DataGrid控件)

            this.AddTabItem("数据", "Silverlight.Common.View.DataGridDemo")

        }

private void treeView_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)

        {

//SilverLight学习笔记六TreeView控件中的TreeViewSample(TreeView控件)

            this.AddTabItem("树视图", "Silverlight.Common.View.TreeViewSample")

        }

private void dataForm_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)

        {

//SilverLight学习笔记七DataPager,DataForm控件的DataForm()

            this.AddTabItem("数据表单", "Silverlight.Common.View.DataFormDemo")

        }

    }

}

5年了,都没人来回答, 技术搜索方面百毒果然不行

先加入Panel, 然后依次加入你要的控件。

StackPanel panel = new StackPanel()

Button button=new Button(){Content="button1"}

TextBlock textblock= new TextBlock(){Text="TextBlock1"}

panel.Children.Add(button)

panel.Children.Add(textblock)

TabItem item= tabcontrol.Items[0] as TabItem

item.Content = panel

https://social.msdn.microsoft.com/Forums/zh-CN/f4d97e7c-2bba-49eb-9367-a96547b6707e/tabitem?forum=wpfzhchs


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11269184.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-14
下一篇 2023-05-14

发表评论

登录后才能评论

评论列表(0条)

保存