我有两个WPF窗口。一个是主窗口,另一个是配置窗口。 我想配置窗口发送和事件到主窗口。 知道我是如何完成的吗?
这是我创建和打开配置窗口的方式:
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
ConfigPage Confwin= new ConfigPage();
Confwin.Owner = this;
Confwin.Show();
}
答案 0 :(得分:18)
您可以在子窗口类中定义任何事件,并在显示窗口之前订阅它。
主窗口
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
Child childWindow = new Child();
childWindow.MyEvent += new EventHandler(childWindow_MyEvent);
childWindow.ShowDialog();
}
void childWindow_MyEvent(object sender, EventArgs e)
{
// handle event
MessageBox.Show("Handle");
}
}
子窗口
public partial class Child : Window
{
// define event
public event EventHandler MyEvent;
protected void OnMyEvent()
{
if (this.MyEvent != null)
this.MyEvent(this, EventArgs.Empty);
}
public Child()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Child_Loaded);
}
void Child_Loaded(object sender, RoutedEventArgs e)
{
// call event
this.OnMyEvent();
}
}
答案 1 :(得分:2)