禁用wpf组合框的项目(如果存在于mysql数据库中)

时间:2018-09-20 10:46:56

标签: c# mysql sql wpf

我在xaml中有以下组合框:

<ComboBox Name="unn" Grid.Column="1" Grid.Row="1" ItemsSource="{Binding}" SelectionChanged="unn_SelectionChanged"/>

每当更改选择时,它将在数据库中检查项目是否已经存在。如果已经存在,那么我要禁用该项目的选择。

这是我检查数据库的方式:

public void chk()
    {
        using (MySqlConnection connection = new MySqlConnection(accessStr))
        {
            connection.Open();
            command = new MySqlCommand("select count(1) as value from tbl1 where productID=" + unid + " and " + "month(current_date) = month(entryDate)", connection);
            reader = command.ExecuteReader();
            while (reader.Read())
            {
                ret = reader.GetInt32(0);

            }

            If(ret==1) {MessageBox.show("Data Already Exist")};

        }
    }

我不确定如何使项目无法选择。

1 个答案:

答案 0 :(得分:0)

您需要回滚选择。

private bool reverting;

private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ComboBox comboBox = (ComboBox)e.Source;
    if (reverting) return;
    bool revert = true | false; // true or false
    if (revert)
    {
        reverting = true;
        comboBox.SetCurrentValue(Selector.SelectedItemProperty, e.RemovedItems.Count != 0 ? e.RemovedItems[0] : null);
        reverting = false;
    }
}
相关问题