0

i am using WPF recently. I am pretty new to C# and WPF. I constructed dynamical a Border with some Child. I want a copy from this border (dynamic and with child) and change only some properties. There seems to be only a default and no copy constructor MSDN Border.

I found a related answer : How Can you Clone a Wpf Object.

Is there a way to avoid to Copy the XAML?

My Question: Is there an inbuild function that constructs a deep copy from a WPF Border. Why is there no Copy Constructor?

Thank you for your time.

Community
  • 1
  • 1
Mehno
  • 868
  • 2
  • 8
  • 21
  • 1
    Consider making it a user control and use dependency properties for the dynamic bits. – Dan Field Mar 08 '16 at 11:02
  • So basically, you want to deep-copy a whole arbitrary WPF node including all child nodes? Since thats what your border can contain. You may be better of, removing the border from your question, since the title looks specific while the solution needs to be generic. – grek40 Mar 08 '16 at 11:23
  • @grek40 You are right. I changed the title. Thank you – Mehno Mar 08 '16 at 11:54
  • 2
    I really hope someone comes along with a generic solution. Until then, a few more thoughts: You should consider the MVVM pattern and just clone your viewmodel. You can adjust your view based on a viewmodel property, by using anything like converters, style/datatriggers, templateselectors, ... – grek40 Mar 08 '16 at 12:21
  • @grek40 There are some really useful things in your comment, but i still can not believe that there is no copy constructor for such a case. – Mehno Mar 09 '16 at 10:51

1 Answers1

0

I want to give two small "why not" examples to the question.

Consider the following XAML snippet:

<DockPanel LastChildFill="False">
    <TextBlock DockPanel.Dock="Right" Text="Test"/>
</DockPanel>

The dependency properties LastChildFill and Text are relatively easy, since they somehow belong to their object.

However, the DockPanel.Dock is an attached property, so the DependencyObject where it is attached to is potentially unaware of its existence. An arbitrary attached property can be attached to an arbitrary dependency object. The property does not belong to the object, but ignoring it on copy would lead to a change in the resulting layout.

Second example:

<Border>
    <ContentPresenter Content="{Binding SomeObject}"/>
</Border>

There may be a DataTemplate for the type of SomeObject, so consequently a visual tree will be constructed, but SomeObject itself is not a WPF specific object, so what is your semantic expectation on getting a deep-copy of the border node?

Generally you may want to be more specific whether you want to deep-copy the logical tree or the visual tree. Your question tends towards the logical tree, but I suspect you are more interested in copying visual presentation of your layout. Consult your favorite search engine for more details on the matter. Here are two random points to start with: MSDN on WPF Trees and WPF tutorial on logical and visual tree.

grek40
  • 13,113
  • 1
  • 24
  • 50