control form from another form in wpf
how to control main form from another form example I want to access the table in main form from another f开发者_Go百科orm in wpf?
You can either use UI automation (which would only let you interact with it as if you were a user clicking on/typing in the controls): http://msdn.microsoft.com/en-us/library/dd561932(VS.85).aspx
Or you can use code behind to pass a reference from one window to the other, probably in your Application class.
There is nothing specific to WPF that makes either option any easier or harder to implement.
salamonti, Do you want to access a control on the main form or the data that the control is displaying? If the latter I would suggest you to keep the data in a separate area than the control presenting it. This can be achieved with MVVM and several other view separation patterns. You can also use Routed Events and Routed Commands to execute code in one "form" from a different one.
If you want to access the "main form" from a child form, you can create a property on the child form of type FrameworkElement for example. Then, when you create the child form, just populate this property with the instance of the main form. This way you'll have access to whatever you want in the main form.
this is a tiny sample of communication between windows in WPF
you can refer to controls as the way you do with class fields, cause that's what they are
public class Form1 : Window
{
public DateTime FormCreationDate {get; set;}
private void button1_Click(object sender, RoutedEventArgs e)
{
Form2 a = new Form2();
a.Owner = this;
a.Show();
}
}
public class Form2 : Window
{
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.label1.Content = string.Format(
"the owner of this window was created on {0}",
((Form1)this.Owner).FormCreationDate.ToString());
}
}
精彩评论