在子窗口設(shè)計(jì)中添加一個(gè)輸入框:<TextBox x:Name="txtUserInfor" Grid.Row="0" />
添加子窗體的CS代碼:
string testString;
public string TestString
{
get { return testString; }
set { testString = value; }
}
增加一個(gè)事件: public event EventHandler OkClicked;
修改OKButton_Click方法:
private void OKButton_Click(object sender, RoutedEventArgs e)
{
if (OkClicked != null)
{
TestString = txtUserInfor.Text;
OkClicked(this,new EventArgs());
}
this.DialogResult = true;
}
父窗體:
設(shè)計(jì)中添加一個(gè)輸入框和一個(gè)Button:
<TextBox x:Name="tbInfo" Width="100" Height="50"></TextBox>
<Button Click="Button_Click" Width="100" Height="60" Content="子窗口測(cè)試" HorizontalAlignment="Right"></Button>
修改父窗體代碼:
public FatherControl()
{
InitializeComponent();
childWindowDemo.OkClicked += new EventHandler(childWindowDemo_OkClicked);
}
void childWindowDemo_OkClicked(object sender, EventArgs e)
{
tbInfo.Text = childWindowDemo.TestString;
}
private ChildWindow1 childWindowDemo = new ChildWindow1();
private void Button_Click(object sender, RoutedEventArgs e)
{
childWindowDemo.Show();
}
完畢。
運(yùn)行,點(diǎn)擊父窗體按鈕,彈出子窗體。在子窗體的輸入框中輸入內(nèi)容,點(diǎn)擊確認(rèn)后關(guān)閉子窗體,同時(shí)子窗體的數(shù)據(jù)更新到父窗體的輸入框中。