0

I have an object of class Employee (let us call it Alice), and want to create a copy of this object (let us call it Alice_again) such that if I change Alice, the properties of Alice_again also changes. Basically creating a copy of an object whose properties are bound to the original.

Edits

I am having two diferent ObservableCollection of Employee(Employee is User Control) and both contains same object Employee and I want to display each ObservableCollection once. For that I am using ItemsControl and I have binded ItemsSource of ItemsControl to these ObservableCollection and as I have read on this link that a given object may only be present in a given logical tree once, so I am trying to create a copy of object but I don't want to update both copies whenever there is an update.

Aditya Jain
  • 33
  • 1
  • 8
  • If all properties are identical, then why a copy? Why can't one copy be used? The goal is not yet clear - it is difficult to propose an optimal solution. – EldHasp Jun 15 '21 at 10:02
  • In this case it sounds like it's not a copy, it's just the original reference? – Charleh Jun 15 '21 at 10:05
  • I am having 2 diferent ObservableCollection of Employee and I want to display each list. For that I am using ItemsControl and as I have read on [this](https://stackoverflow.com/questions/6105309/wpf-net4-0-re-use-same-instance-of-usercontrol) link that a given object may only be present in a given logical tree once. @EldHasp – Aditya Jain Jun 15 '21 at 10:10
  • Please show use case (you can [edit] question to add missing details). I guess you simply need to pass reference of Alice around. – Sinatr Jun 15 '21 at 10:10
  • 2
    Two lists may contain references to the same object. The elements in the ItemsSource collection should not be Visuals. – Clemens Jun 15 '21 at 10:11
  • @Clemens ItemsSource for ItemsControl are the ObservableCollection and dataTemplate has buttons whose content is binded to properties of Employee – Aditya Jain Jun 15 '21 at 10:21

2 Answers2

2

You do not need to create a copy.

Since the elements in the ItemsSource collection are not supposed to be Visuals, two or more source collections may contain references to the same objects.

Two different UI elements in the ItemTemplate of two ItemsControl would hence legitimately bind to the same data item object.

Clemens
  • 123,504
  • 12
  • 155
  • 268
  • I just tested it and it worked. Thank you so much for the help!! – Aditya Jain Jun 15 '21 at 12:07
  • @AdityaJain It doesn't really matter. You should accept the answer that most helped you. If two answers are equally helpful, accept the "better" one, whatever that means :-) – Clemens Jun 15 '21 at 12:14
0

object may only be present in a given logical tree once

logical tree is a tree of UI elements. Elements of a different type are not included in this tree.
For each item in the source collection, its own ContentPresenter will be created, which will be included in the logical tree of the parent ItemsControl.
And each ItemsControl creates its own collection from the ContentPresenter.
Therefore, different ContentPresenters will be created in different ItemsControls for the same source item and this will not create any problems.
You can even include the same item multiple times in the same collection. And for each position of this element, its own ContentPresenter will be created.

dataTemplate has buttons whose content is binded to properties of Employee

A data template is, in fact, a factory for creating UI elements. And when applying the template for each element, its own unique UI elements will be created.

EldHasp
  • 6,079
  • 2
  • 9
  • 24
  • Oh ok, so own unique UI elements will be created for repeated entry as well? I mean if same object is added twice so unique UI element will be created twice or once? @EldHasp – Aditya Jain Jun 15 '21 at 12:09
  • Twice. Try specifying a simple collection int [] {1,1,1,1}. Each 1 will be in its own line and this does not create any problems – EldHasp Jun 15 '21 at 12:25
  • Oh ok, got it! Thank you so much !! – Aditya Jain Jun 15 '21 at 12:59