简体   繁体   English

组合框更改显示的项目c#

[英]Combobox change the displayed item c#

I have a combobox and a Dictionary as Binding Source from wich i can choose an item and equip an instance with the selected item 我有一个组合框和一个字典作为绑定源,我可以选择一个项目并为实例配备所选项目

comboBoxNutzungsart.DataSource = new BindingSource(Zordnung.dieListe, 
comboBoxNutzungsart.DisplayMember = "Key";
comboBoxNutzungsart.ValueMember = "Value";

lets assume I chose the same instance which I already gave a value, now I want the combobox to show me the item. 让我们假设我选择了一个已经给定值的实例,现在我希望组合框向我显示该项目。

Example of what I mean: Person has the atrribute colour and the combobox contains the items "green, yellow, red, blue" at first the attribute for colour is for every person null. 我的意思示例:人具有特殊的颜色,并且组合框首先包含项“绿色,黄色,红色,蓝色”,每个人的颜色属性均为null。 (up untill here there is no problem) (直到这里没有问题)

I chose for Person 1 the colour red. 我为第一个人选择了红色。 when I choose Person 1 again I want the combobox to show me the colour red. 当我再次选择“人员1”时,我希望组合框显示红色。

I think there might be problem becaus of the BindingSource. 我认为BindingSource可能会出现问题。

for better a better understanding: 为了更好地理解:

....
public class person 
   public int personNumber
   public double personColourValue
   public string personColour

....
public static Dictionary<string, double>ListColourValue = new Dictionary<string, double> {
{"green", 50},
{"red",  60},
{"yellow", 70}
};

....
cmb.DataSource = new BindingSource(ListColourValue,mull)
cmb.DisplayMember = "Key";
cmb.ValueMember="Value";

....

for personColour= null and person p1 对于personColour = null和person p1

private void cmb_SelectedIndesChandes (object sender, EventArgs e]
{
var selectedItem = (KeyValuePair<string, double> cmb.SelectedItem;
string colour = selectedItem.Key;
double cvalue = selectedItem.Value;
p1.personColourValue = cvalue;
p1.personColour = colour;
}
......

为此,您需要将人的颜色值存储在某个变量中,然后将其与组合框绑定。它将解决您的问题。

Add Color property to Person. 将“颜色”属性添加到“人”。 Every time color is selected, add to Color property of current Person. 每次选择颜色时,将其添加到当前“人”的“颜色”属性中。

Every time Person changed, set color of combobox with color of selected Person. 每次更改人员时,请使用所选人员的颜色设置组合框的颜色。

Of course, Person List should be stored in somewhere in DB or others else. 当然,人员列表应该存储在数据库中的某个位置或其他位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM