使用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;
            }
        }
相关推荐
FuckPatience2 天前
WPF GridView 列绑定时前端没有属性提示
wpf
界面开发小八哥2 天前
界面控件DevExpress WPF v26.1新版亮点 - 模板工具包 & MVVM功能全新升级
wpf·界面控件·devexpress·ui开发·.net 10
记忆停留w4 天前
Celery+Redis 分布式异步任务队列工程落地业务逻辑
大数据·人工智能·redis·分布式·缓存·架构·wpf
雪靡4 天前
WPF PerMonitorV2 下的跨窗口问题
c#·wpf
listening7775 天前
HarmonyOS 6.1 分布式文件服务实战:跨设备商品图片/视频秒开方案
wpf·harmonyos
Macbethad5 天前
基于WPF的半导体设备EAP控制程序架构设计与实现
wpf
国服第二切图仔6 天前
HarmonyOS APP《画伴梦工厂》开发第60篇-分布式软总线2.0——多设备协同新范式
分布式·wpf·harmonyos
精神底层6 天前
拒绝 UI 卡死!我用 CSnakes 实时拦截 Python 训练流,在 WPF 中重构了 ScottPlot 5 高性能 AI 看板
python·ui·wpf
爱吃大芒果6 天前
不用多设备模拟器跑通 ShareKit 碰一碰分享
华为·wpf·harmonyos
贪玩的小松鼠7 天前
WPF基础到企业应用系列7——深入剖析依赖属性(WPF/Silverlight核
wpf