ChildWindow:
private string _test; public string Test //测试属性 { get { return _test; } set {_test = value; } } public event EventHandler OkBtnClicked;//新建事件,在点击ok button时,事件初始化 private void OKButton_Click(object sender, RoutedEventArgs e) { if (OkBtnClicked != null) { OkBtnClicked(this, new EventArgs()); _test = "test"; } this.DialogResult = true;//只有在true时窗口才可以关闭,如果为fals }
FatherWindow:
ChildWindow1 childwin = new ChildWindow1();//针对子窗口的引用 public MainPage() { InitializeComponent(); childwin.Closed += new EventHandler(childwin_Closed); //监听子窗口的关闭,在要求不高时,个人觉得也可以采用这种较为简单的方法 childwin.OkBtnClicked += new EventHandler(childwin_OkBtnClicked);//监听子窗口的ok按钮 } private void childwin_OkBtnClicked(object sender, EventArgs e) { MessageBox.Show(childwin.Test); } private void childwin_Closed(object sender, EventArgs e) { MessageBox.Show(childwin.Test); }