Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Combobox无法准确获取数据库值_C#_Mysql_Wpf_Combobox - Fatal编程技术网

C# Combobox无法准确获取数据库值

C# Combobox无法准确获取数据库值,c#,mysql,wpf,combobox,C#,Mysql,Wpf,Combobox,我正在尝试将windows窗体应用程序迁移到wpf,但我遇到了麻烦 当使用组合框时,会显示SelectedIndexChanged事件已被SelectionChanged替换为wpf等效事件 我的应用程序连接到一个MySQL数据库,从中获取所有信息。 我的特定组合框由数据库表中的字段填充。 想法是; 选择一个组合框项目,其他文本框应显示同一行的相应值。 相反,情况就是这样 背后的代码: private void Domain_SelectionChanged(object sender

我正在尝试将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">

它的工作方式与WWW中SelectedIndexChanged事件的工作方式相同。我只是想不出一个方法来在wpf中正确地传输它。 非常感谢您的帮助。
(请忽略“stmp”输入)

我会检查控件的XAML绑定。查看更新资源记录器的设置,以确保事件向下流到事件处理程序

<ListView x:Name="RolesListView"
   ItemsSource="{Binding Path=RoleOptionListMember, 
                         ValidatesOnDataErrors=True, 
                         UpdateSourceTrigger=PropertyChanged}"
   SelectionMode="Single"
   ScrollViewer.VerticalScrollBarVisibility="Auto"
   Focusable="True">


我不确定这是否有帮助,因为每个人都在向您展示问题的代码,但根据我的经验,如果您使用数据集,您可以非常轻松地解决此问题。如果使用“数据源”选项卡,则可以使用它创建所有要使用的表的数据集。然后在数据集中找到表,并将要使用的属性拖放到组合框中。如果操作正确,应该不会有问题。

如果从数据库中正确填充组合框,则不需要任何SelectionChanged事件。您可以将组合框中选定项的字段绑定到文本框,如下所示:

<ComboBox x:Name="DomainCB" ItemsSource="{Binding}" />

文本框:

<TextBox Text="{Binding Path=SelectedItem.domainid, ElementName=DomainCB}" />
<TextBox Text="{Binding Path=SelectedItem.smtp, ElementName=DomainCB}" />
<TextBox Text="{Binding Path=SelectedItem.port, ElementName=DomainCB}" />
<TextBox Text="{Binding Path=SelectedItem.security, ElementName=DomainCB}" />


绑定后设置selectedIndex=0data@Binson这很好。。它做了些什么。但它抛出了一个例外;“使用方法‘mysql_native_password’对用户‘root’的主机‘localhost’进行身份验证失败,消息为:用户‘root’@‘localhost’的访问被拒绝(使用密码:YES)”。考虑到我已经用代码隐藏中的设置参数打开了连接,我觉得这很奇怪。我认为您应该在绑定到上述组合框的所有文本框中尝试:UpdateSourceTrigger=“PropertyChanged”。这是另一个与Db访问相关的错误。跟踪程序的执行并找到错误发生的地方
我正在尝试将windows窗体应用程序迁移到wpf
-首先学习MVVM并从代码隐藏中删除所有可怕的SQL相关代码。UI不是放置数据库访问代码的正确位置。了解软件具有,并且您不能将所有内容都放在Window1.xaml.cs中。