Reputation: 35
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.
Upvotes: 0
Views: 154
Reputation: 7918
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.
Upvotes: 0
Reputation: 128060
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.
Upvotes: 2