c# - 在属性树上使用 IWindowsFormsEditorService 进行下拉列表不会关闭

标签 c# winforms propertygrid selectedindexchanged listbox-control

所以我有一个像这篇文章的答案一样的设置: display list of custom objects as a drop-down in the PropertiesGrid ,我有一个属性树,我需要其中一个属性树有一个下拉列表,我从字符串数组中填充该下拉列表。

我正在使用 IWindowsFormsEditorService 来获得下拉菜单。我创建列表框:

 IWindowsFormsEditorService service = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
ListBox lb = new ListBox();
lb.SelectionMode = SelectionMode.One;
lb.SelectedIndexChanged += OnListBoxSelectedValueChanged;
lb.Items.AddRange(myStringArray)
service.DropDownControl(lb);

我有 OnListBoxSelectedValueChanged 事件:

service.CloseDropDown();

但是下拉菜单没有关闭,我不知道为什么。在那之前它工作正常。

从我发现的其他代码示例来看,它应该可以工作。如果我单击属性树上的其他位置,它会关闭,但为什么它不会在 SelectedIndexChanged 上关闭? listBox 控件是否有覆盖它的东西?除了上面的内容之外,我没有设置其他 listBox 属性。

最佳答案

您使用的服务变量是 EditValue 方法的本地变量。它与您在 OnListBoxSelectedValueChanged 中使用的实例不同。

您可以这样简单地替换事件处理程序:

var service = (IWindowsFormsEditorService)provider
    .GetService(typeof(IWindowsFormsEditorService));
var lb = new ListBox();
lb.SelectionMode = SelectionMode.One;
lb.SelectedIndexChanged += (obj, args) => { service.CloseDropDown(); };

关于c# - 在属性树上使用 IWindowsFormsEditorService 进行下拉列表不会关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48812515/

相关文章:

C# - 将焦点传递给 tabcontrol/页面并且无法滚动鼠标滚轮

c# - 我流泪了,因为我不知道如何用 Action 现场初始化 Dictionary?

javascript - ExtJS PropertyGrid - 动态设置源

c# - PropertyGrid 中的 UITypeEditor 具有多选功能

c# - 在 .Net 中实现并行任务队列

c# - 为什么 Math.Round() 不支持 float ?

c# - 带有背景图像的 WinForms 分层控件在滚动时导致撕裂

c# - 删除/编辑 TabPages 的 TabControl 填充

c# - 无闪烁文本框

c# - 将 PropertyGrid 默认 View 设置为 "category view"