Reputation: 115
I have the main window, and another window. in the 2nd window i've created new canvas
, and i want to change its properties from the main window, i've failed with this try:
this is the 2nd window's class:
public partial class window2 : Window
{
public Canvas painting = new Canvas();
public window2()
{
}
}
and here i try to change its property from the main window:
window2 paint = new window2();
private void button1_Click(object sender, RoutedEventArgs e)
{
paint.painting.Background = Brushes.Black;
}
when i click the button it does nothing.
Edit:
i think it would be better if i'll use the Application.current.properties and store the canvas object, but i don't know how to use it, i tried this:
Application.Current.Properties["p1"] = painting;
now how can i set properties from the main window with using the "p1" variable i've just created? i tried p1.background
but i cant use p1 as variable, so how do i do it?
Upvotes: 1
Views: 253
Reputation: 127
Your window2
constructor should contain this:
this.AddChild(painting);
Whenever you create a new control (like Canvas
) you should set its parent container.
This is my code from window2:
public Canvas painting = new Canvas();
public window2()
{
this.AddChild(painting);
}
And mainwindow:
private void button1_Click(object sender, RoutedEventArgs e)
{
window2 w = new window2();
w.Show();
w.painting.Background = Brushes.Black;
}
What I believe you are saying is that you have an undetermined number of canvases and you want to access them all. I suggest you keeping them in a List
of canvases or in a HashTable
(you need to use System.Collections
namespace). Also don't forget to set the parent container.
Upvotes: 1
Reputation: 178660
A Canvas
is a little odd as far as WPF controls go. It likely has a zero size, so you're not seeing the change. Try hard-coding a size to check whether the code is working. In the window2
constructor do this:
painting.Width = 100;
painting.Height = 100;
Upvotes: 0