什么是克隆在WPF中的TabItem最简单的方法? [英] What's the easiest way to clone a TabItem in WPF?

查看:448
本文介绍了什么是克隆在WPF中的TabItem最简单的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有需要克隆所有的选项卡的内容,以反映的WPF TabItem的当前状态,完全preserving所有绑定。要求是,每个选项卡应独立运作有自己的一批相同的控制。到目前为止,我只设法创建空白标签。 MSDN表明,这是不可能的。似乎是为MS错过pretty的基本功能,因此这可能吗?

  //定位的TabControl的标签将被添加到
TabControl的itemsTab =(TabControl的)this.FindName(TabControl的);

//创建并填充新的选项卡并将其添加到标签控件
TabItem的newTab =新的TabItem(); //这应该不是克隆现有标签名为mainTab
newTab.Content =细节;
newTab.Header =名称;
itemsTab.Items.Add(newTab);

//显示新选项卡给用户;如果此行缺少
//你得到一个空白标签
itemsTab.SelectedItem = newTab;
 

解决方案

 公共主窗口()
    {
        的InitializeComponent();
        TabItem的TAB2 = TrycloneElement(TAB1);
        如果(TAB2!= NULL)
            main.Items.Add(TAB2);
    }




    公共静态牛逼TrycloneElement< T>(T原稿)
    {
        尝试
        {
            字符串s = XamlWriter.Save(原稿);

            StringReader stringReader =新StringReader(S);

            的XmlReader的XMLReader = XmlTextReader.Create(stringReader,新XmlReaderSettings());
            XmlReaderSettings SX =新XmlReaderSettings();

            对象x = XamlReader.Load(XmlReader的);
            返程(T)×;
        }
        抓住
        {
            返回(T)((对象)空);
        }

    }
 

XAML

 < TabControl的宽度=500X:名称=主>
        < TabItem的标题=AMTAB1X:名称=TAB1>
            < TextBlock的文本=blalbla>< / TextBlock的>< / TabItem的>
    < / TabControl的>
 

I have the need to clone all of the contents of a tab to reflect the current state of a WPF TabItem, fully preserving all the bindings. The requirement is that each Tab should function independently with its own group of identical controls. I've so far only managed to create blank tabs. MSDN suggests this isn't possible. Seems like pretty basic functionality for MS to miss, so is this possible?

//    locate the TabControl that the tab will be added to
TabControl itemsTab = (TabControl) this.FindName("tabControl");

//    create and populate the new tab and add it to the tab control
TabItem newTab = new TabItem(); //This should instead clone an existing tab called "mainTab"
newTab.Content = detail;
newTab.Header = name;
itemsTab.Items.Add(newTab);

//    display the new tab to the user; if this line is missing
//    you get a blank tab
itemsTab.SelectedItem = newTab;

解决方案

 public MainWindow()
    {
        InitializeComponent();
        TabItem tab2 = TrycloneElement(tab1);
        if (tab2 != null)
            main.Items.Add(tab2);
    }




    public static T TrycloneElement<T>(T orig)
    {
        try
        {
            string s = XamlWriter.Save(orig);

            StringReader stringReader = new StringReader(s);

            XmlReader xmlReader = XmlTextReader.Create(stringReader, new XmlReaderSettings());
            XmlReaderSettings sx = new XmlReaderSettings();

            object x = XamlReader.Load(xmlReader);
            return (T)x;
        }
        catch
        {
            return (T)((object)null);
        }

    }

XAML

    <TabControl Width="500" x:Name="main">
        <TabItem Header="AMTAB1" x:Name="tab1">
            <TextBlock Text="blalbla"></TextBlock></TabItem>
    </TabControl>

这篇关于什么是克隆在WPF中的TabItem最简单的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆