public partial class SubWindow : Window
{
public delegate void PassValuesHandler(object sender, PassValuesEventArgs e);
public event PassValuesHandler PassValuesEvent;
public SubWindow()
{
InitializeComponent();
}
private void btnOK_Click(object sender, RoutedEventArgs e)
{
string value1 = tbValue1.Text; // Text Property return value is string type .
int value2;
Int32.TryParse(tbValue2.Text, out value2);
PassValuesEventArgs args = new PassValuesEventArgs(value1, value2);
if ( PassValuesEvent!=null)
{
PassValuesEvent(this, args);
}
this.Close();
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnOpenSubWindow_Click(object sender, RoutedEventArgs e)
{
SubWindow subWindow = new SubWindow();
// 订阅事件
subWindow.PassValuesEvent += new SubWindow.PassValuesHandler(ReceiveValues);
subWindow.Show();
}
private void ReceiveValues(object sender, PassValuesEventArgs e)
{
this.tbValue1.Text = e.Value1;
this.tbValue2.Text = e.Value2.ToString();
}
}