c# - 方法更改的字符串值未显示在数据绑定(bind)文本框中

标签 c# mysql wpf xaml

所以我有一个类和一个表格。 该类托管 MySQL 代码,表单保存触发 MySQL 类中代码的事件

// Code that acceses the class
// This triggers a method that is supposed to get the next record from the database.
private void next_Click(object sender, RoutedEventArgs e)
    {
        // Tag.text is the auto incremented unique record number.
        // The MySQL code is meant to get the next record ahead of the number in tag.text
        // in the corresponding table field.
        usersMysql.RegForm_Next(tag.Text);
    }

下一部分是访问 MySQL 代码以获取下一条记录的方法

public void RegForm_Next(string tag_Value)
    {
        // tagValue now holds the number, which was in tag.text in the previous page, as a string
        // tagValue has already been predeclared as a string
        tagValue = tag_Value;
        // Navigation is the method that holds the MySQL code.
        // By passing "Forward", the method has a code to tell from that, which query to excecute.
        Navigation("Forward");
    }

下一个代码是用于获取记录的 MySQL 代码

// Command to go to the next or previous rexord
    public void Navigation(string scroll)
    {

        if (scroll == "Forward")
        {
            query = "select * from temp.signup where tag = (select min(tag) from temp.signup where tag > '" + tagValue + "' );";
        }
        if (scroll ==  "Backward")
        {
            query = "select * from temp.signup where tag = (select max(tag) from temp.signup where tag < '" + tagValue + "' );";
        }

        //Database connection parameters
        string sqlcon = "datasource = " + datasource + ";" + "port=" + port + ";" + "username=" + username + ";" + "password=" + password + ";";
        MySqlConnection con = new MySqlConnection(sqlcon);

        MySqlDataReader rdr;
        MySqlCommand cmd = new MySqlCommand(query, con);

        //Excecution
        try
        {
            //If the connection is Open
            con.Open();
            {
                rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    // Declarations
                    // All these strings have been declared under the public class declaration.
                    sid = GetString(rdr, "id");
                    stag = GetColumnValueAsString(rdr, "tag");

                    sfirst = GetString(rdr, "first");
                    sfourth = GetString(rdr, "surname");

                    sdob = rdr.GetString("dob");
                    ssex = rdr.GetString("sex");

                    susername = GetString(rdr, "username");
                    spassword = GetString(rdr, "password");

                }
                con.Close();
            }
        }

        catch (Exception ex)
        {
            ModernDialog.ShowMessage(ex.Message, "SQL related error: Nav", MessageBoxButton.OK);
        }

    }

现在问题就来了。我需要将字符串值绑定(bind)到上一页中调用 MySQL 类中的方法的文本框。

// This is how the ID binding was set up for example.
// This is where sid was declared.
string sid;
public string ID
    {
        get
        {
            return sid;
        }

        set
        {
            sid = value;
            RaisePropertyChanged("ID");
        }
    }

这就是文本框绑定(bind)的设置方式

<TextBox x:Name="id" Text="{Binding ID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged }" Margin="158,46,453,468" FontSize="13" TabIndex="1" />

我已经在页面中设置了文本框的数据上下文,但字符串永远不会加载回文本框

public registrationForm()
    {
        InitializeComponent();

        usersMysql = new users.MySQL.usersMySQL();
        DataContext = usersMysql;
    }

我可以确认字符串正在加载。我已经用消息框进行了测试。但文本框中没有显示任何内容。

这是我在类中用于“propertyChanged”的函数

//Property changed
    private void RaisePropertyChanged(string prop)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(prop));
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;

如果我将两个文本框绑定(bind)到同一个公共(public)字符串,它们会反射(reflect)在另一个文本框中键入的内容。那我哪里出错了?

最佳答案

您的代码正在更改 sid 支持字段,而不是 ID 属性。

因此,PropertyChanged 事件永远不会触发,WPF 也永远不会发现更改。

关于c# - 方法更改的字符串值未显示在数据绑定(bind)文本框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24511837/

相关文章:

mysql - 在 Doctrine QueryBuilder 中,尝试根据相关实体记录的数量选择记录

wpf - 将 ImageSource 转换为 BitmapImage - WPF

c# - 什么可能导致作业无法完成?

mysql - 删除前导零并将值插入到新列中

MySQL 错误代码 1005 errno -1

c# - 如何在 XAML 中显示我的枚举?

wpf - 在 ViewModel 中获取窗口属性

C#确认消息框在拖放过程中卡住文件资源管理器

c# - C#中的文件创建

c# - 从 C# 运行 Bash 命令