从* new *组合框文本而不是旧文本更新

时间:2019-05-09 12:38:38

标签: c# mysql wpf combobox

我有一个组合框,我正在从中选择一个值。我想使用组合框中新选择的值作为与数据库的比较,但是我只能返回与选择更改之前组合框中的文本匹配的值。

我尝试将.text更改为选定的项目和选定的值,但这两个都不返回选择。

这在组合框选择更改时被调用:

    private void StationComboBox_1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        GetIP();
    }

    private void GetIP()
    {
        string connectionString = "SERVER=localhost;DATABASE=db; UID=PC;Password=pw;";

        MySqlConnection connection = new MySqlConnection(connectionString);

        MySqlCommand cmd = new MySqlCommand("SELECT IP_stations from stations WHERE stationNumber_stations='" + stationComboBox_1.Text + "'", connection); 
        connection.Open();

        string IP = (string)cmd.ExecuteScalar();


        DisplayIP.Text = IP;

这是该ComboBox中的代码,它从相同的数据库但从不同的表中获取其值。

<ComboBox 
    Grid.Column="1" 
    x:Name="stationComboBox_1" 
    FontSize="25" 
    Width="60" 
    HorizontalAlignment="Left" 
    DisplayMemberPath="stationNumber_stations" 
    ItemsSource="{Binding}" 
    Text="0" 
    SelectionChanged="StationComboBox_1_SelectionChanged"
    />

DisplayIP Xaml:

<TextBox x:Name="DisplayIP"/>

设置组合框:

        public void SQLSetup()
        {
            string connectionString = "SERVER=localhost;DATABASE=db; UID=PC;Password=pw;"; 

            MySqlConnection connection = new MySqlConnection(connectionString); 

            MySqlCommand cmd = new MySqlCommand("Select stationNumber_stations from stations", connection); //Command to select all the station numbers
            connection.Open();


            DataTable dt = new DataTable(); //Creates a new data table
            dt.Load(cmd.ExecuteReader()); //Loads the data table with the values returned from the MySQL command
            connection.Close(); //Closes the MySQL connection

            //Sets the values in the station dropdowns to the values from the data table with the station numbers
            stationComboBox_1.DataContext = stationComboBox_2.DataContext = stationComboBox_3.DataContext = stationComboBox_4.DataContext = stationComboBox_5.DataContext =
                stationComboBox_6.DataContext = stationComboBox_7.DataContext = stationComboBox_8.DataContext = dt;
            stationComboBox_1.ItemsSource = stationComboBox_2.ItemsSource = stationComboBox_3.ItemsSource = stationComboBox_4.ItemsSource = stationComboBox_5.ItemsSource =
                stationComboBox_6.ItemsSource = stationComboBox_7.ItemsSource = stationComboBox_8.ItemsSource = dt.DefaultView;


            //sets an int to the value selected in the station dropdown
            string stationSelection_1 = stationComboBox_1.Text;
            string stationSelection_2 = stationComboBox_2.Text;
            string stationSelection_3 = stationComboBox_3.Text;
            string stationSelection_4 = stationComboBox_4.Text;
            string stationSelection_5 = stationComboBox_5.Text;
            string stationSelection_6 = stationComboBox_6.Text;
            string stationSelection_7 = stationComboBox_7.Text;
            string stationSelection_8 = stationComboBox_8.Text;
        }

我希望DisplayIP文本框显示当前选择的IP。但是,它显示的是先前选择的IP。

现在,在启动时,ComboBox和TextBox都为空。我在ComboBox中选择一个工作站,并且TextBox保持为空。当我在组合框中选择一个新电台时,文本框将更新以显示第一个选择的IP地址。

1 个答案:

答案 0 :(得分:1)

当您在“组合框”中更改选择时,您希望文本框显示所选工作站的IP地址。我们知道它在台表的IP_stations列中。

我们已经讨论了ItemsSource="{Binding}"是多余的事实,因此我将其省略。我省略了选择更改的处理程序,因为该工作将通过绑定来完成。选择更改时,无需重新查询数据库,因为我们已经在组合框中显示了DataView中所有行的所有列。

您已经学会了DisplayMemberPath,我们将使用他的邪恶双胞胎SelectedValuePath

SelectedValuePath="IPStations"告诉组合框,当选择更改时,它应查看选定的项(在本例中为DataView的DataRowView),并尝试查找属性或列(在本例中为列)用那个名字。如果找到它,则ComboBox会将该列的值分配给它自己的SelectedValue属性。然后,我们教文本框通过将其Text属性绑定到stationComboBox_1.SelectedValue来进行自我更新。

要执行此操作,您需要从表中选择两个值(我第一次忽略了这个值):

//Command to select all the station numbers and IPs
MySqlCommand cmd = 
    new MySqlCommand("Select stationNumber_stations, IP_stations from stations", 
        connection); 

完成。

<ComboBox 
    Grid.Column="1" 
    x:Name="stationComboBox_1" 
    Width="60" 
    HorizontalAlignment="Left" 
    DisplayMemberPath="stationNumber_stations" 
    SelectedValuePath="IP_stations"
    />
<TextBox
    Text="{Binding SelectedValue, ElementName=stationComboBox_1}" 
    />

ComboBox.Text仅在组合框的文本可编辑的情况下在WPF中使用。

顺便说一下,这是在选择更改处理程序中可以完成的操作:

private void stationComboBox_1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var cb = (sender as ComboBox);

    //  If SelectedValuePath is "IP_Stations", this will be the IP address. 
    //  It will be the correct current selected value. 
    var selectedValue = cb.SelectedValue;

    //  Alternatively, we could do it this way:
    var row = cb.SelectedItem as DataRowView;

    if (row != null)
    {
        var selectedIP = row["IP_stations"];
    }
}

至少在我看来,这里的下一个问题是您将所有内容复制和粘贴了八次。 MVVM可以使您摆脱困境。