使用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;
            }
        }
相关推荐
码界奇点1 天前
基于eBPF技术的高性能网络防火墙系统设计与实现
开发语言·网络·毕业设计·php·wpf·go语言·源代码管理
cjp5601 天前
022.WPF 封装TextBox控件限制只输入数字自定义属性
wpf
cjp5601 天前
021.WPF 以MVVM模式控制combox控件显示/隐藏
wpf
小北方城市网2 天前
Redis 分布式锁高可用实现:从原理到生产级落地
java·前端·javascript·spring boot·redis·分布式·wpf
流水线上的指令侠2 天前
补充说明——针对《C#:从 0 到 1 创建基于 NUnit + FlaUI 的 WPF UI 自动化测试项目》
功能测试·ui·c#·自动化·wpf
流水线上的指令侠2 天前
C# 实战:从 0 到 1 搭建基于 NUnit + FlaUI 的 WPF UI 自动化测试项目
功能测试·ui·c#·自动化·wpf·visual studio
贾修行2 天前
.NET 全栈开发学习路线:从入门到分布式
c#·.net·wpf·asp.net core·web api·winforms·services
晓13132 天前
第四章:Redis实战应用及常见问题(下篇)
java·数据库·缓存·wpf
掘根3 天前
【jsonRpc项目】客户端的Requestor模块,RpcCaller模块
wpf
FuckPatience3 天前
WPF ListBoxItem绑定自己在ListBox中的顺序
wpf