简体   繁体   English

如何将主 WIndow 文本框的值传递给其他用户控件?

[英]How to pass value from Main WIndow textbox to other User Control?

Aim:目标:

I am aiming to pass value from main window ie the log in screen to other User Control forms.我的目标是将值从主 window 即登录屏幕传递到其他用户控件 forms。

This is MainWindow.Xaml.cs这是MainWindow.Xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void CloseButton_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
    }


    private void btnLogin_Click(object sender, RoutedEventArgs e)
    {

        string connString = ConfigurationManager.ConnectionStrings["Technical_Application.Properties.Settings.ConnectionString"].ConnectionString;
        string query = "SELECT count(*) from users where username = '" + txtUsername.Text + "' and password = MD5('" + txtPassword.Password + "')";

        using (var conn = new MySqlConnection(connString))
        {
            conn.Open();
            using (var cmd = new MySqlCommand(query, conn))
            {
                int count = Convert.ToInt32(cmd.ExecuteScalar());
                if(count == 1)
                {
                    Dashboard dashboard = new Dashboard();
                    dashboard.Show();
                    this.Close();
                }
                else
                {
                    MessageBox.Show("Username or Password unvalid", "Login Error", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }
            conn.Close();
        }
    }
}

It opens up the dashboard .它打开了仪表板 I now have a ListViewMenu directed to specific User Control forms, and opens on the dashboard.我现在有一个指向特定User Control forms 的ListViewMenu ,并在仪表板上打开。

Code to open different user controls打开不同用户控件的代码

   private void ListViewMenu_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        int index = ListViewMenu.SelectedIndex;
        MoveCursorMenu(index);

        switch (index)
        {
            case 0:
                GridPrincipal.Children.Clear();
                GridPrincipal.Children.Add(new UserControlMain());

                break;
            case 1:
                GridPrincipal.Children.Clear();
                GridPrincipal.Children.Add(new UserControl1());
                break;
            default:
                break;
        }

    }

Question:问题:

From MainWindow the text box with name of txtUsername how can I transfer the text value to other UserControl windows?MainWindow名称为txtUsername的文本框如何将文本值传输到其他 UserControl windows?

Wouldnt it be easy to add a string property in your Dashboard class, and pass it into that - like:在您的仪表板 class 中添加字符串属性并将其传递给该属性不是很容易 - 比如:

Dashboard dashboard = new Dashboard();
dashboard.UserName = txtUsername.Text;

Then you can pass the same string value down to other controls.然后您可以将相同的字符串值传递给其他控件。 Of course, ideally, you would implement the MVVM pattern and use bindings.当然,理想情况下,您将实现 MVVM 模式并使用绑定。

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

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