2011-05-06 74 views
0

我与基于对象的阵列上的数据源的组合框,以及Value属性绑定到在模型信息库中的属性:检测所述数据的无效数据绑定源

DataSource = someArray; 
ValueMember = "ArrayValue"; 
DisplayMember = "Name"; 
DataBindings.Add("Value", repository, "RepositoryValue"); 
DataBindings["Value"].DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged; 

在某些情况下用户更改组合框中的选定项目,它反映在repository.RepositoryValue中,有时我们从文件或数据库中提取数据,并直接填充repository.RepositoryValue,然后自动反映在combox框中。有时文件或数据库可能包含无效值(someArray中未包含的值),我们希望检测该场景并强制组合框选择列表中的第一项或完全拒绝该更改。这是可能的,我们应该怎么做呢?

回答

0

您可以使用Binding的Format事件来处理它。

Binding SelectedValueBinding = new Binding("SelectedValue", repository, "RepositoryValue", true, DataSourceUpdateMode.OnPropertyChanged); 
SelectedValueBinding.Format += new ConvertEventHandler(SelectedValueBinding_Format); 
myComboBox.DataBindings.Add(SelectedValueBinding); 

void SelectedValueBinding_Format(object sender, ConvertEventArgs e) 
{ 
     // if e.Value is Invalid 
     // myComboBox.SelectedValue = "Default Value"; 
} 

检查更多:

How does one databind a custom type to TextBox.Text?