我在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")};
}
}
我不确定如何使项目无法选择。
答案 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;
}
}