views:

260

answers:

1

I have a bunch of UserControls ('MyUserControl') that I want to have a user manually add to one or more Canvases. One instance of a UserControl cannot be a child element of more than one container (otherwise a 'System.InvalidOperationException: Element is already the child of another element.' is thrown).

Is there a way to do this without creating new (duplicate) instances of the MyUserControls? If not, what would be the best strategy to keep duplicate instances of the MyUserControls in sync?

+4  A: 

You cannot add the same instance of a control (any control) as a child of more than one parent. You will need to create multiple instances of your Usercontrol and place each on its own parent.

If you need to keep the data they display in sync then you should store that data in an object separate from the user controls themselves, its this object that you would only have one instance of. Typically you would assign this data object to the DataContext property of each user control, then the various component parts of the User control can get their data using data binding.

If you ensure that your data object implements INotifyPropertyChanged correctly then when one User control makes a change to the data it will be reflected in all the other User Controls referencing the same data object.

AnthonyWJones