【问题标题】:Method changed string values are not showing up in data-bound textboxes方法更改的字符串值未显示在数据绑定文本框中
【发布时间】:2014-07-01 13:44:30
【问题描述】:

所以我有一个类和一个表格。 该类承​​载 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);
        }

    }

现在问题就出在这里。我需要将字符串值绑定到上一页中的文本框,该页面调用了 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");
        }
    }

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

<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;

如果我将两个文本框绑定到同一个公共字符串,它们会反映在另一个中输入的内容。那我哪里错了?

【问题讨论】:

    标签: c# mysql wpf xaml


    【解决方案1】:

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

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

    【讨论】:

    • 现在我只是觉得自己很愚蠢。我很惊讶没有人对此投反对票。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2012-07-24
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-15
    相关资源
    最近更新 更多