【问题标题】:disable items of wpf combobox if exist in mysql database如果 mysql 数据库中存在,则禁用 wpf 组合框的项目
【发布时间】:2019-02-24 15:52:14
【问题描述】:

我在 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")};

        }
    }

我不知道如何使一个项目不可选择。

【问题讨论】:

  • “禁用”是什么意思?不显示还是...?
  • 您当前代码的确切问题是什么?
  • X39, Disable mean, Not selectable !
  • @kodr:所以你想在一个项目已经被选中后禁用它的选择?为什么不查询数据库一次,然后禁用从该查询返回的所有项目,而不是每次在 ComboBox 中选择新值时查询数据库?

标签: c# mysql sql wpf


【解决方案1】:

您需要回滚选择。

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;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-19
    相关资源
    最近更新 更多