English 中文(简体)
Silverlight: Add same UserControl to more than one Canvas
原标题:

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?

最佳回答

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.

问题回答

暂无回答




相关问题
Silverlight: Add same UserControl to more than one Canvas

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 (...

c++ templated container scanner

here s today s dilemma: suppose I ve class A{ public: virtual void doit() = 0; } then various subclasses of A, all implementing their good doit method. Now suppose I want to write a function ...

Multi-Dimensional Container in Java

I searched around on Google, but I was unable to find any libraries for a multi-dimensional container in Java (preferably one that supports generics as well). I could easily write one (in fact, I ...

c# stack queue combination

is there in C# some already defined generic container which can be used as Stack and as Queue at the same time? I just want to be able to append elements either to the end, or to the front of the ...

Custom container requirement to work with Qt s foreach

What is the bare minimum amount of code to create a custom container that would work with Qt foreach macro? I have this so far template< class T > class MyList { public: class iterator { ...

Having many stacks with different types

I m making a C program that needs to use two stacks. One needs to hold chars, the other needs to hold doubles. I have two structs, node and stack: struct node { double value; struct node *...

热门标签