I’m trying to have the content of two windows be shared, but it doesn’t seem to be working as i would expect.
Here is a simple example showing this:
class TestClass
{
[STAThread]
public static void Main(string[] args)
{
Application app = new Application();
TextBox t = new TextBox();
t.Text = "test";
Window w1 = new Window();
w1.Content = t;
w1.Show();
Window w2 = new Window();
w2.Content = t;
w2.Show();
app.Run();
}
}
If i step through the program in debug mode, i can see that w1 has the textbox, but as soon as the call w2.Content = t; then w1 loses the textbox… and it then shows up in only w2.
I also tried using binding in Xmal, where both windows pointed to the same ViewModel (with a property named MySharedContent)
Content="{Binding Path=MySharedContent}"
but it doesn’t work.
Is this possible? Am i doing something stupid? I was thinking that both windows would have a pointer to the content for rendering… but it almost seems like they can’t share the same instance?
EDIT:
So the basic answer is that a UI element (textbox, usercontrol, etc) can only have 1 parent. Thus i need to have two separate UI elements that share the same associated data. Got it (thanks everyone for the help, up-votes for everyone)
Controls have a
Parentproperty which returns the container they have been placed in. Therefore and because eachTextBoxneeds to have its own set of properties like its size and location, your approach cannot work. You can share the value (the string"test"in your case) displayed by theTextBoxesin the two windows, but not theTextBoxesthemselves.Create two Textboxes!