你怎么刷新就地组合框项目?组合、你怎么、项目

2023-09-04 01:22:24 作者:倾一世旧梦

组合框的项目集合是一个ObjectCollection,所以你当然可以将想要的有什么,但是这意味着你没有得到就像你使用,也就是说,一个ListViewItem的Text属性。组合框通过调用toString()方法在每个项目上,或者使用反射,如果DisplayMember属性设置显示的项目。

The ComboBox Items collection is an ObjectCollection, so of course you can store anything you want in there, but that means you don't get a Text property like you would with, say, a ListViewItem. The ComboBox displays the items by calling ToString() on each item, or using reflection if the DisplayMember property is set.

我的组合框在DropDownList的模式。我有一个情况下,我想刷新单个项目的项目文本列表中时,得到由用户选择。问题是,组合框不会重新查询,随时文本之外时,它加载了,我想不通怎么回事做什么,我想,除了删除和重新添加,像这样选择的项目:

My ComboBox is in DropDownList mode. I have a situation where I want to refresh the item text of a single item in the list when it gets selected by the user. The problem is that the ComboBox doesn't re-query for the text at any time besides when it loads up, and I can't figure out how else to do what I want besides removing and re-adding the selected item like so:


PlantComboBoxItem selectedItem = cboPlants.SelectedItem as PlantComboBoxItem;

// ...

cboPlants.BeginUpdate();

int selectedIndex = cboPlants.SelectedIndex;
cboPlants.Items.RemoveAt(selectedIndex);
cboPlants.Items.Insert(selectedIndex, selectedItem);
cboPlants.SelectedIndex = selectedIndex;

cboPlants.EndUpdate();

这code正常工作,除了我的SelectedIndex事件最终被解雇两次(一次在原来的用户事件,然后当我再次重新设置在这个code中的财产)的事实。在这种情况下,这不是什么大不了的事,该事件被触发两次,但它的效率不高,而且我讨厌这样。我可以搭起一个标志,以便在退出事件中的第二次,但那是黑客。

This code works fine, except for the fact that my SelectedIndex event ends up getting fired twice (once on the original user event, and then again when I re-set the property in this code). In this case, it's not a big deal that the event is fired twice, but it's inefficient, and I hate that. I could rig up a flag so it exits the event the second time, but that's hacking.

有没有更好的方式来得到这个工作?

Is there a better way to get this to work?

推荐答案

明白了,用甜甜圈的建议。

Got it, using Donut's suggestion.

在窗体类:

private BindingList<PlantComboBoxItem> _plantList;

在加载方式:

_plantList = new BindingList<PlantComboBoxItem>(plantItems);
cboPlants.DataSource = _plantList;

在SelectedIndexChanged事件:

In SelectedIndexChanged event:

int selectedIndex = cboPlants.SelectedIndex;
_plantList.ResetItem(selectedIndex);

感谢您!

 
精彩推荐
图片推荐