简体   繁体   English

无法弄清楚如何从单独的.xaml窗口更新文本框控件

[英]Cannot figure out how to update a textbox control from a separate .xaml window

The question implies I'm brand new to WPF, and I am having a lot of problems so far transitioning from winforms. 这个问题意味着我是WPF的新手,到目前为止,从Winforms过渡到现在,我遇到了很多问题。

What I'm trying to do is a very simple concept, but I'm spinning in circles trying to get it to work. 我想做的是一个非常简单的概念,但是我正在兜圈子,试图使其发挥作用。 I want to have a textbox on my MainWindow update from a property on my 2nd window. 我想在MainWindow上从第二个窗口的属性更新一个文本框。 However, this window is not open all the time. 但是,此窗口并非一直都打开。

In MainWindow, I open up a new window from an event like so: 在MainWindow中,我通过如下事件打开一个新窗口:

private void menuChangeSerialPort_Click(object sender, RoutedEventArgs e)
    {
        ChangeSerialPort changeSerialPort = new ChangeSerialPort();
        changeSerialPort.Show();
    }

The purpose of the new window is to give the user the option to change serial ports. 新窗口的目的是为用户提供更改串行端口的选项。 I scan them and put them into a combo box. 我扫描它们并将其放入组合框。 Upon selecting one and confirming (pressing an 'OK' button), I want to send back the string of the serial port name for the MainWindow.xaml to use in the future. 选择一个并确认(按“ OK”(确定)按钮)后,我想发回MainWindow.xaml的串行端口名称字符串,以备将来使用。 I close my 2nd window after that: 之后,我关闭第二个窗口:

public partial class ChangeSerialPort : Window
{
    public ChangeSerialPort()
    {
        InitializeComponent();

        // Used with XAML, the owner is set so the opening position is centered according to where the Main Window is.
        this.Owner = Application.Current.MainWindow;

        // Scan and list port names
        GetSerialPorts(); 
    }

    private string _portname;
    public string serialPortName { get { return _portname; } } // Readonly property


private void serialPortOKBtn_Click(object sender, RoutedEventArgs e)
    {
        txt_noSerialSelected.Visibility = Visibility.Hidden;
        if (cmbbox_serialPortList.SelectedItem == null)
        {
            txt_noSerialSelected.Visibility = Visibility.Visible;
        }
        else
        {
            _portname = cmbbox_serialPortList.SelectedItem.ToString();
           //
           // WHAT TO PUT??
           //
            this.Close();
        }
    }

I cannot for the life of me get data binding to work. 我一生都无法获得数据绑定功能。 I understand this is the proper way, but I have spent a number of hours reading and getting no where. 我知道这是正确的方法,但是我已经花了几个小时阅读,却一无所获。 My backup plan is to access the data from a property. 我的备份计划是从属性访问数据。 But I do not know how to access any properties back on MainWindow, or vice versa (because the window will close after the selection is confirmed). 但是我不知道如何访问MainWindow上的任何属性,反之亦然(因为确认选择后窗口将关闭)。 So I'm 0/2. 所以我是0/2。

Can anyone help me out? 谁能帮我吗? And possibly explain in very simple terms how databinding could help me out in this scenario? 并可能用非常简单的术语来说明在这种情况下数据绑定可以如何帮助我? For instance, what are the steps to take and what exactly would go into the textbox xaml on my MainWindow? 例如,要采取什么步骤,以及将要进入MainWindow上的文本框xaml的确切内容是什么?

<TextBox x:Name="serialPortInUse" Text="?????????" HorizontalAlignment="Right" Margin="0" VerticalAlignment="Top" Height="20" Width="86" TextChanged="serialPortInUse_TextChanged" BorderThickness="0"/>

Thanks in advance! 提前致谢!

Any number of ways of doing this, but the simplest (from your POV) is: 这样做的方法有很多,但是最简单的方法(从POV中来看)是:

  1. The second window raises an event when the port is selected. 选择端口后,第二个窗口将引发一个事件。 The eventargs would contain the name of the selected port. eventargs将包含所选端口的名称。
  2. When the main window creates second one, it subscribes to the event, using a new method to receive the event. 当主窗口创建第二个窗口时,它使用新方法接收事件,订阅该事件。
  3. When the main window receives the event, set sets the text of serialPortInUse 当主窗口接收到事件时,set设置serialPortInUse的文本

This is fine for a little app (in the sense of not many windows) as this looks to be. 看起来不错,这对于一个小应用程序(在没有很多窗口的情况下)来说是很好的。 In more complicated apps, I recommend you use the MVVM pattern. 在更复杂的应用程序中,建议您使用MVVM模式。 There is a very good book about it, Advanced MVVM , which has been around a while now but is still a superb introduction. 关于它的一本很好的书是Advanced MVVM ,它已经存在了一段时间,但仍然是很棒的介绍。 With MVVM you can use frameworks like MVVMLite - which you can also install via NuGet install-package MVVMLight - which has a Messaging system built in which decouples senders from listeners and would be a natural in this situation. 借助MVVM,您可以使用诸如MVVMLite之类的框架(也可以通过NuGet install-package MVVMLight进行install-package MVVMLight ,该框架具有内置的消息传递系统,该系统可将发送者与监听者分离,在这种情况下是很自然的。

Add a ref parameter in your ChangeSerialPort window; 在您的ChangeSerialPort窗口中添加一个ref参数;

public partial class ChangeSerialPort : Window
    {
        TextBox textBox;
        public ChangeSerialPort( TextBox myTextBox)
        {
            InitializeComponent();
            textBox = myTextBox;
        }
    }

Pass that textbox 通过该文本框

private void menuChangeSerialPort_Click(object sender, RoutedEventArgs e)
{
    ChangeSerialPort changeSerialPort = new ChangeSerialPort(serialPortInUse);
    changeSerialPort.Show();
}

set the text value 设置文字值

private void serialPortOKBtn_Click(object sender, RoutedEventArgs e)
        {
            txt_noSerialSelected.Visibility = Visibility.Hidden;
            if (cmbbox_serialPortList.SelectedItem == null)
            {
                txt_noSerialSelected.Visibility = Visibility.Visible;
            }
            else
            {
                _portname = cmbbox_serialPortList.SelectedItem.ToString();
                textBox.Text = _portname;
                this.Close();
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM