我在 Silverlight 中有一个子窗口,我希望发送一个字符串值来填充应用程序 MainPage.xaml 中的文本框。
我怎样才能将值传回?
我试过这个 -
MainPage m = (MainPage)Application.Current.RootVisual;
m.textBox1.Text = value;
我在 Silverlight 中有一个子窗口,我希望发送一个字符串值来填充应用程序 MainPage.xaml 中的文本框。
我怎样才能将值传回?
我试过这个 -
MainPage m = (MainPage)Application.Current.RootVisual;
m.textBox1.Text = value;
你应该反过来做。打开子窗口的父级应将事件处理程序附加到子级的事件中,例如:
childwindow.ButtonClicked += new EventHandler(childWindow_ButtonClicked);
在这个处理程序中,Parent 可以使用来自子 Window 属性的值更新它自己的 Controls。
private void childWindow_ButtonClicked(object sender, EventArgs e)
{
txtValue.Text = childwindow.Value;
}
假设您使用的是 mvvm 模式,您可以使用子窗口的 ShowDialog 方法来打开它。ShowDialog 方法一直等到窗口关闭。
窗口关闭后,您可以从窗口视图模型中读取依赖属性并在主页中设置它们的值。
var view = new ChildWindowView();
var model = new ChildWindowViewModel();
view.DataContext = model;
var result = view.ShowDialog();