Pass Data from Child Window to MainWindow TextBlock

前端 未结 5 660
臣服心动
臣服心动 2021-01-26 10:35

In this example, MainWindow has a button that opens Window2.

Window2 has a button that writes \"Hello, World!\" to MainWindo

5条回答
  •  滥情空心
    2021-01-26 11:14

    Another Approach

    MainWindow

     public partial class MainWindow : Window
        {
    
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private bool DisplayText(string displayText)
            {
                txt_Main.Text = displayText;
                return true;
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                Window2 win2 = new Window2(DisplayText);
                win2.ShowDialog();
            }
        }
    

    Window2

     public partial class Window2 : Window
        {
            private Func mainWindowMethod;
    
            public Window2(Func displayMethod)
            {
                InitializeComponent();
                this.mainWindowMethod = displayMethod;
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                this.mainWindowMethod("Hello World");
            }
        }
    

提交回复
热议问题