Windows窗体组合框 - 数据绑定到多个属性 [英] Windows Forms combobox - databind to multiple properties
问题描述
我有麻烦数据绑定到一个ComboBox的SelectedValue的和Text属性。下面是相关代码片段:
DataTable的DT1 = DataAccess.GetLoanPurposes();
ddLoanPurpose.DisplayMember =姓名;
ddLoanPurpose.ValueMember =值;
ddLoanPurpose.DataBindings.Add(文本,_scenario,LoanPurposeString,FALSE);
ddLoanPurpose.DataBindings.Add(的SelectedValue,_scenario,LoanPurpose,FALSE);
ddLoanPurpose.DataSource = DT1;
我可以绑定到任何文本或的SelectedValue,一切工作出色。麻烦的是当我尝试绑定到两个。仅第一数据绑定作品(文本到LoanPurposeString在上面的例子)。如果我切换数据绑定...
ddLoanPurpose.DataBindings.Add(的SelectedValue,_scenario的顺序,LoanPurpose ,FALSE);
ddLoanPurpose.DataBindings.Add(文本,_scenario,LoanPurposeString,FALSE);
...然后我失去了文本LoanPurposeString的结合,但现在的SelectedValue绑定到LoanPurpose。所以我的问题是双重的:为什么不能同时绑定线到我_scenario对象,为什么顺序关系
公共KeyValuePair<字符串,字符串> LoanPurpose {搞定;组; }
我只需要确保在数据源的ValueMember变量是一个KeyValuePair。因此,像...
DataTable的DT =新的DataTable();
dt.Columns.Add(新的DataColumn(名,typeof运算(字符串)));
dt.Columns.Add(新的DataColumn(值的typeof(KeyValuePair<字符串,字符串>)));
DataRow的卓尔= dt.NewRow();
卓尔[名称] =埃里克;
卓尔[价值] =新KeyValuePair<字符串,字符串>(1,埃里克);
dt.Rows.Add(卓尔);
绑定语法将是一样的我原来的问题,唯一的区别是,ValueMember是现在一个KeyValuePair对象,而不是字符串
ddLoanPurpose.DisplayMember =姓名;
ddLoanPurpose.ValueMember =值;
ddLoanPurpose.DataSource = DT;
I am having trouble databinding to a Combobox's SelectedValue and Text properties. Here is the relevant code snippet:
DataTable dt1 = DataAccess.GetLoanPurposes();
ddLoanPurpose.DisplayMember = "Name";
ddLoanPurpose.ValueMember = "Value";
ddLoanPurpose.DataBindings.Add("Text", _scenario, "LoanPurposeString", false);
ddLoanPurpose.DataBindings.Add("SelectedValue", _scenario, "LoanPurpose", false);
ddLoanPurpose.DataSource = dt1;
I can bind to either Text or SelectedValue and everything works brilliantly. The trouble comes when I try to bind to both. Only the first Databinding works (Text to LoanPurposeString in the example above). If I switch the order of the Databinding...
ddLoanPurpose.DataBindings.Add("SelectedValue", _scenario, "LoanPurpose", false);
ddLoanPurpose.DataBindings.Add("Text", _scenario, "LoanPurposeString", false);
...then I lose the binding of Text to LoanPurposeString but now SelectedValue binds to LoanPurpose. So my question is twofold: Why don't both of the bindings wire up to my _scenario object and why does the order matter?
Databinding to two different properties doesn't seem to be supported (at least not based on my research). In this particular instance, the best solution is to combine the separate properties in my _scenario object (LoanPurpose and LoanPurposeString) into a single KeyValuePair property.
public KeyValuePair<string, string> LoanPurpose { get; set; }
I just need to make sure that the ValueMember variable in the DataSource is a KeyValuePair. So something like...
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Name", typeof(string)));
dt.Columns.Add(new DataColumn("Value", typeof(KeyValuePair<string, string>)));
DataRow dRow = dt.NewRow();
dRow["Name"] = "Eric";
dRow["Value"] = new KeyValuePair<string, string>("1", "Eric");
dt.Rows.Add(dRow);
The binding syntax will be the same as in my original question, the only difference is that the ValueMember is now a KeyValuePair object instead of a string.
ddLoanPurpose.DisplayMember = "Name";
ddLoanPurpose.ValueMember = "Value";
ddLoanPurpose.DataSource = dt;
这篇关于Windows窗体组合框 - 数据绑定到多个属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!