什么是克隆在WPF中的TabItem最简单的方法?最简单、方法、WPF、TabItem

2023-09-08 08:34:39 作者:一小我、演习一小我?

我有需要克隆所有的选项卡的内容,以反映的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的>
 
WPF 中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>