方法更改的字符串值未显示在数据绑定的文本框中
本文关键字:数据绑定 文本 显示 字符串 方法 | 更新日期: 2023-09-27 18:11:11
我有一个类和一个表单。类托管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类up方法的文本框。
// 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;
如果我将两个textbox绑定到同一个公共字符串,它们将反映在另一个中键入的内容。那么我错在哪里呢?
您的代码正在更改sid
支持字段,而不是ID
属性。
因此,PropertyChanged
事件不会被触发,WPF也不会发现更改