简体   繁体   English

TabControl深度复制

[英]TabControl Deep Copy

I need to make a deep copy of a tabcontrol. 我需要复制一个tabcontrol。

The large picture is this: I have a project which has a 300 line XAML code TabControl with 8 tabs in it which are pretty big. 大图是这样的:我有一个项目,其中有300行XAML代码TabControl,其中有8个选项卡,它们很大。 I also have a TreeView with different items. 我也有一个带有不同项目的TreeView。

When an item in that list is selected it shows the TabControl associated with it. 在该列表中选择一个项目时,它会显示与其关联的TabControl。 The problem now comes that when I add an element I want to make a deep copy of the original TabControl and associate that new one to the new element (of course I'm going to erase the content in the new one). 现在出现的问题是,当我添加一个元素时,我想对原始TabControl进行深拷贝并将该新元素与新元素相关联(当然,我将擦除新元素中的内容)。 Shallow copies won't work because they are pointing to the same location in memory, so "=",IClonable are a no go. 浅拷贝将不起作用,因为它们指向内存中的相同位置,因此“ =,IClonable”是行不通的。 And the frustrating part is that I can't use deep copy with serialization because the TabControl is not serializable. 而且令人沮丧的是,我无法将深度复制与序列化一起使用,因为TabControl不可序列化。 And I can't (or should say won't) make a custom TabControl which is serializable because the TabControl is 300 line in XAML and it would be 600 line in code so it's a waste of space and time. 而且我不能(或者应该说不会)制作一个可序列化的自定义TabControl,因为TabControl在XAML中为300行,而在代码中则为600行,因此浪费了空间和时间。

I've searched for this for 2 days and didn't find anything. 我已经搜索了2天,没有找到任何东西。 There is no need for me to show the code because I'm looking for a general purpose Deep Copy method that can copy any type of a TabControl. 无需显示代码,因为我正在寻找可以复制任何类型的TabControl的通用Deep Copy方法。

You're doing it all wrong. 你做错了一切。

You're not supposed to "copy" the UI or whatever, because UI is Not Data . 您不应该“复制” UI或其他任何东西,因为UI不是Not Data You should be manipulating and copying Data Items instead of UI elements, and probably using DataTemplate s to have your Data items represented on screen. 您应该操作和复制数据项而不是UI元素,并且可能使用DataTemplate来在屏幕上显示数据项。

I strongly suggest that you research and learn MVVM before ever writing a single line of code in WPF. 我强烈建议您在用WPF编写一行代码之前先研究和学习MVVM。

After searching some more I initially tried a different way but it turned out to be more trouble than it's worth (tried with data binding and working more in code but still to much). 在搜索了更多内容之后,我最初尝试了另一种方法,但结果却发现麻烦多于其应有的价值(尝试了数据绑定并在代码中工作更多,但仍然很多)。

So the solution is to use XamlReader and XamlWriter. 因此解决方案是使用XamlReader和XamlWriter。 Official documentation is found here and respectively here ! 官方文档位于此处此处

To answer my question in code it would be: Say you had this tabcontrol in XAML here: 要在代码中回答我的问题,将是:假设您在XAML中具有以下tabcontrol:

<TabControl>
  <TabItem><!--A lot of stuff here--></TabItem>
  <TabItem><!--More stuff here--></TabItem>
</TabControl>

And remember this is if you have a lot of stuff (basically I didn't mention this but I'm making an interface that creates a pretty complex XML so in that TabControl I'm handling a lot of user generated data)! 记住,这是如果您有很多东西(基本上我没有提到这一点,但是我正在创建一个创建非常复杂的XML的接口,因此在TabControl中,我要处理大量用户生成的数据)! If you have a simple TabControl just make a custom one in code or simply use DataBindings. 如果您有一个简单的TabControl,只需在代码中创建一个自定义控件即可,或仅使用DataBindings。

So the code in the background for making a Deep Copy of that XAML defined TabControl would be this: 因此,用于对该XAML定义的TabControl进行深度复制的代码如下:

string savedTabControl = XamlWriter.Save(originalTabControl);

StringReader stringReader = new StringReader(savedTabControl);
XmlReader xmlReader = XmlReader.Create(stringReader);
TabControl newTabControl = (TabControl)XamlReader.Load(xmlReader);

So this is basically serialization made on xaml controls and not on data. 因此,这基本上是在xaml控件而非数据上进行的序列化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM