所以我有一个班级和一个表格。 该类承载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;
如果我将两个textBox绑定到同一个公共字符串,它们会反映另一个文本字符串的输入内容。那我哪里错呢?
答案 0 :(得分:2)
您的代码正在更改sid
支持字段,而不是ID
属性。
因此,永远不会触发PropertyChanged
事件,WPF永远不会发现更改。