【发布时间】:2014-01-21 00:26:00
【问题描述】:
我正在尝试将 Windows 窗体应用程序迁移到 wpf,但我碰壁了。
在使用组合框时出现,SelectedIndexChanged 事件已被 SelectionChanged 替换,因为它与 wpf 等效。
我的应用程序连接到一个 MySQL 数据库,它可以在其中获取所有信息。 我的一个特定组合框由数据库表中的一个字段填充。 这个想法是; 选择一个组合框项,其他文本框应显示同一行的相应值。 相反,这就是发生的事情。
后面的代码:
private void Domain_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Set connection parameters
string sqlcon = "datasource = localhost; port = 3306; username = root; password = localhost";
// Set query to excecute
string query_fetch = "select * from mail.smtp where domain = '" + this.Domain.Text + "';";
// declaratons
MySqlConnection con = new MySqlConnection(sqlcon);
MySqlCommand cmd_fetch = new MySqlCommand(query_fetch, con);
MySqlDataReader rdr;
// Excecution of command
try
{
con.Open();
rdr = cmd_fetch.ExecuteReader();
while (rdr.Read())
{
// Declarations
string sdomainid = rdr.GetInt32("domainid").ToString();
string ssmtp = rdr.GetString("smtp");
string sport = rdr.GetString("port");
string ssecurity = rdr.GetString("security");
// Bindings
Domain_ID.Text = sdomainid;
SMTP.Text = ssmtp;
port.Text = sport;
security.Text = ssecurity;
}
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
xaml:
<ComboBox x:Name="Domain" SelectedValue="-1" Margin="186,132,0,0" Height="18" VerticalAlignment="Top" HorizontalAlignment="Left" Width="211" FontSize="11" SelectionChanged="Domain_SelectionChanged" TabIndex="4">
它的工作方式与 wWinForms 中的 SelectedIndexChanged 事件一样。我似乎无法找到一种在 wpf 中正确翻译它的方法。 任何帮助深表感谢。 (请忽略“stmp”错字)
【问题讨论】:
-
绑定数据后设置 selectedIndex=0
-
@Binson Eldhose 好吧.. 它做了一些事情。但是它抛出了一个异常; “使用方法'mysql_native_password'对用户'root'的主机'localhost'进行身份验证失败并显示消息:用户'root'@'localhost'的访问被拒绝(使用密码:YES)”。考虑到我已经在后面的代码中使用设置参数打开了连接,我觉得这很奇怪..
-
我认为您应该尝试:UpdateSourceTrigger="PropertyChanged" 在绑定到上述组合框的所有文本框中。
-
这是另一个与数据库访问相关的错误。跟踪程序的执行并找到错误发生的地方
-
I'm trying to migrate a windows form app to wpf- 从学习 MVVM 开始,并从后面的代码中删除所有可怕的 SQL 相关代码。 UI 不是放置数据库访问代码的正确位置。了解该软件有Layers,并且您不能将所有内容都放在 Window1.xaml.cs 中。