Pass value from child window back to MainPage

守給你的承諾、 提交于 2019-12-22 15:03:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!