使用WPF将window 添加到TabControl里面的方法

首先

1.window 不能添加到其他控件中,原因是他是最高级的。。

在window usercontrol,以及page,frame 基本都遵循这个道理,可以添加的则是 除window以外的其他窗体。

2.添加到TabControl 下面的TabItem 控件添加usercontrl我遇到的问题是,控件是有window控件手动转成usercontrol 其中有个参数是错误的,无法试下自适应大小:后来手动修改即可

而window下面的操作是:

差别还是挺大的。

3.适用的方法:

csharp 复制代码
 public static bool  AddTabcontrol(List<string> TabKeys, TabControl ThisTBC, UserControl ThisWindows)
        {
            string TagInfo = ThisWindows.Tag.ToString();
            string[] ThisTagShowArr = TagInfo.Split(',');
            if (ThisTagShowArr.Length == 2)
            {
                string ThisKey = ThisTagShowArr[0];
                string NamaPath = ThisTagShowArr[1];
                if (!TabKeys.Contains(ThisKey))
                {
                    TabItem ThisItem = new TabItem();
                    ThisItem.Header= ThisKey;
                    ThisItem.Name = ThisKey;
                    /* ThisItem.Content=  ThisWindows;  */                  //Uri MainTragetUri = new Uri(NamaPath, UriKind.RelativeOrAbsolute);

                    //Frame ThisFrm = new Frame();
                    //ThisFrm.Name = "frameMaim"; 
                    //ThisFrm.NavigationUIVisibility = System.Windows.Navigation.NavigationUIVisibility.Hidden;
                    //ThisFrm.JournalOwnership = System.Windows.Navigation.JournalOwnership.UsesParentJournal;
                    //ThisFrm.Source = MainTragetUri;
                    //ThisFrm.Navigate(ThisWindows);
                    ThisItem.Content = ThisWindows;
                    ThisTBC.Items.Add(ThisItem);
                    ThisTBC.SelectedItem = ThisItem;
                    TabKeys.Add(ThisKey);
                }
                else
                {
                    int IndexNo = TabKeys.IndexOf(ThisKey);
                    foreach (TabItem OneItem in ThisTBC.Items)
                    {
                        if (OneItem.Name == ThisKey)
                        {
                            ThisTBC.SelectedIndex = IndexNo;
                        }
                    }
                }
                return true;
            }
            else
            {
                return false;
            }
           
           
        }

4.本数据是从网上抄袭下来的:

csharp 复制代码
   public List<TabItem> tabItemsList = new List<TabItem>();
        public TabControl tabDynamic = null;
tabDynamic = queryControl.GetChildObject<System.Windows.Controls.TabControl>(layOut, "tabDynamic");  //获取TabControl 控件
public void AddTabItem(string uriName)
        {
            int count = tabItemsList.Count;
            // create new tab item
            TabItem tab = new TabItem();
            tab.Header = string.Format("Tab {0}", uriName.Split('/')[1]);
            tab.Name = string.Format("tab{0}", uriName.Split('/')[1]);
            tab.HeaderTemplate = tabDynamic.FindResource("TabHeader") as DataTemplate;
            //tab.Background = new SolidColorBrush(Colors.Transparent);
            //tab.MouseDoubleClick += new MouseButtonEventHandler(tab_MouseDoubleClick);
            Uri MainTragetUri = new Uri(string.Format("/Freed.Api.Monitor;component/View/{0}.xaml", uriName), UriKind.RelativeOrAbsolute);
            Frame frame = new Frame();
            frame.Name = "frameMaim";
            frame.NavigationUIVisibility = System.Windows.Navigation.NavigationUIVisibility.Hidden;
            frame.JournalOwnership = System.Windows.Navigation.JournalOwnership.UsesParentJournal;
            frame.Source = MainTragetUri;
            tab.Content = frame;
            // insert tab item right before the last (+) tab item
            var tabOld = (from t in tabItemsList where t.Name == tab.Name select t).FirstOrDefault();
            if (tabOld == null)
            {
                if (count > 0)
                {
                    tabItemsList.Insert(count - 1, tab);
                }
                else
                {
                    tabItemsList.Add(tab);
                }
                tabDynamic.SelectedItem = tab;
            }
            else
            {
                tabDynamic.SelectedItem = tabOld;
            }
        }
相关推荐
dotent·2 小时前
一个 WPF 文档和工具窗口布局容器
wpf
c#上位机2 小时前
wpf之ComboBox
wpf
lindexi6 小时前
WPF 引用 ASP.NET Core 的 AOT 版本
wpf·asp.netcore
我好喜欢你~1 天前
WPF---数据模版
wpf
hqwest2 天前
C#WPF实战出真汁07--【系统设置】--菜品类型设置
开发语言·c#·wpf·grid设计·stackpanel布局
hqwest2 天前
C#WPF实战出真汁08--【消费开单】--餐桌面板展示
c#·wpf·ui设计·wpf界面设计
orangapple2 天前
WPF 打印报告图片大小的自适应(含完整示例与详解)
c#·wpf
三千道应用题3 天前
WPF&C#超市管理系统(6)订单详情、顾客注册、商品销售排行查询和库存提示、LiveChat报表
开发语言·c#·wpf
✎ ﹏梦醒͜ღ҉繁华落℘4 天前
开发WPF项目时遇到的问题总结
wpf
hqwest5 天前
C#WPF实战出真汁06--【系统设置】--餐桌类型设置
c#·.net·wpf·布局·分页·命令·viewmodel