WPF passing text from one window to another window

与世无争的帅哥 提交于 2021-02-19 05:35:09

问题


I have a main window and it has a button. When I press the main window button, it will open sub-window. In sub window, I have a text box and when I enter the text and click the add button in sub-window, the text should display on the main window. How do I achieve this? Thanks in advance.


回答1:


You can create parameterized constructor of MainWindow class and pass value of Textbox from subwindow to main window and in MainWindow you can set content to that label. Here is the implementation MainWindow.xmal.cs

public partial class MainWindow : Window
    {
    public MainWindow ()
        {
        InitializeComponent();
        }
    public MainWindow (string text) : this()
    {
        label.Content = text;

    }
    private void button_Click (object sender, RoutedEventArgs e)
        {
        Window1 win1 = new Window1();
        win1.Show();
        this.Close();
        }
    }

Here is the code for subWindow i.e. Window1.xaml.cs

public partial class Window1 : Window
    {
    private string text;
    public Window1 ()
        {
        InitializeComponent();
        }

    private void button_Click (object sender, RoutedEventArgs e)
        {
        text = textBox.Text;
        MainWindow mainWindow = new MainWindow(text);
        mainWindow.Show();
        this.Close();
        }
    }



回答2:


I would add an event to your 'dialog' subwindow which the MainWindow can subscribe to as it instantiates it.

DialogWindow: (subwindow):

public class DialogInputEventArgs : EventArgs
{
    public string Input { get; set; }
}

public partial class DialogWindow : Window
{
    public event EventHandler<DialogInputEventArgs> InputChanged = delegate { };

    private void SubmitInputButton_Click(object sender, RoutedEventArgs e)
    {
        InputChanged(this, new DialogInputEventArgs() { Input = this._inputTextBox.Text });
    }
}

MainWindow:

private void ShowDialogButton_Click(object sender, RoutedEventArgs e)
{
    DialogWindow dw = new DialogWindow();
    dw.InputChanged += OnDialogInputChanged;
    dw.Show();
}

private void OnDialogInputChanged(object sender, DialogInputEventArgs e)
{
    // update the MainWindow somehow using e.Input (the text submitted in dialog)
}

If you need this mechanism for multiple windows I would go with something more generic, like a messagebus or observerpattern thing.




回答3:


You can simply modify the constructor of the second window :

   public partial class Window1 : Window
   {
     string text;
     public Window1 (string _text)
     {
     InitializeComponent();
     this.text = _text;
     }
   }



回答4:


I'd suggest you to use CaliburnMicro framwework to achieve easier and better communication between your controls (assuming you are using MVVM pattern). You don't have to implement all features of CaliburnMicro, just EventAggregator to manage sending and handling messages between your controls.



来源:https://stackoverflow.com/questions/46089017/wpf-passing-text-from-one-window-to-another-window

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