2

すべてのバインディングを完全に保持しながら、WPF TabItem の現在の状態を反映するために、タブのすべてのコンテンツを複製する必要があります。要件は、各タブが独自の同一のコントロール グループで独立して機能することです。これまでのところ、空白のタブしか作成できませんでした。MSDN は、これが不可能であることを示唆しています。MS が見逃しているかなり基本的な機能のように思えますが、これは可能ですか?

//    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;
4

2 に答える 2

3
 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>
于 2012-12-19T13:12:31.237 に答える
0

完全なタブページを複製したいのはなぜですか?

MVVM を使用して ui をデータから分離するデータのビューモデルがある場合は、データのクローンを作成するだけで済みます。これはより簡単で、よりクリーンなデザインであり、ビジュアル ツリーや親などの問題を回避できます。

したがって、コードは new Maintab(){DataContext=MainData.Clone()} のようなものになる可能性があります

于 2012-12-19T11:58:53.573 に答える