问题
I have a child window in Silverlight and I wish to send a string value to populate a text box in the applications MainPage.xaml.
How can I pass the value back?
I have tried this -
MainPage m = (MainPage)Application.Current.RootVisual;
m.textBox1.Text = value;
回答1:
You should do this the other way around. The parent that opens the child window should attach an event handler to an event of the child, for example:
childwindow.ButtonClicked += new EventHandler(childWindow_ButtonClicked);
Within this handler, the Parent can update its own Controls with values from properties of the child Window.
private void childWindow_ButtonClicked(object sender, EventArgs e)
{
txtValue.Text = childwindow.Value;
}
回答2:
Assuming that you are using the mvvm pattern, you could use the ShowDialog method of the child window to open it. The ShowDialog method waits until the window is closed.
After the window is closed, you can read the dependent properties from the window viewmodel and set their values in the mainpage.
var view = new ChildWindowView();
var model = new ChildWindowViewModel();
view.DataContext = model;
var result = view.ShowDialog();
来源:https://stackoverflow.com/questions/13194684/pass-value-from-child-window-back-to-mainpage