为自定义控件创建有效属性值的下拉列表
我创建了一个具有多个属性的自定义用户控件。其中之一指定我希望控件访问哪个数据库。我希望能够向控件的用户提供一个下拉菜单,他可以从中选择控件将与哪个数据库交互。
如何让下拉菜单发挥作用?我可以获得默认值,但尚未弄清楚如何获取可选列表。
任何帮助表示赞赏。
谢谢。
马歇尔
I've created a custom user control that has several properties. One specifies which database I want the control to access. I want to be able to present the user of the control a drop down from which he can select which database the control will interact with.
How do I get the dropdown to work? I can get default values, but have yet to figure out how to get the selectable list.
Any help is apprectiated.
Thanks.
Marshall
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您只需将自己的 TypeConverter 附加到您的属性即可。您将重写 GetStandardValuesSupported 和 GetStandardValues 方法(也可能是 GetStandardValuesExclusive)以返回要显示的数据库列表。
如果您不熟悉 PropertyGrid 和 TypeConverter,这里有一个文档。
You just need to attach your own TypeConverter to your property. You will override the GetStandardValuesSupported and GetStandardValues methods (maybe GetStandardValuesExclusive too) to return the list of databases you want to show.
If you are new to the PropertyGrid and the TypeConverter, here is a document.
事实证明比我想象的要简单。
我为该属性设置了枚举,但在将其用于属性类型时遇到问题。说在课堂之外无法访问。
然后我愣了一下,将枚举从 Friend 更改为 Public,然后我就可以使用枚举作为属性类型。因此,当我查看控件的该属性的值时,枚举中的值会列在下拉列表中。
感谢所有回答。
马歇尔
It turns out to be simpler than I thought.
I had an enumeration set up for the property, but was having trouble using it for the property type. Said it was unaccessable outside of the class.
Then I had a 'duh' moment and changed the enumeration from Friend to Public, and then I was able to use the enumeration as the property type. As a result the values from the enumeration are listed in a dropdown when I look at the values for that property of the controls.
Thanks to all that answered.
Marshall
我对你的问题有点困惑。
如果您的用户控件包含 DropDownList 控件,只需在用户控件中的某个位置进行初始化即可。
最简单的方法是在用户控件的代码隐藏中,只需执行 DropDownList.Items.Add() 或任何用于添加项目的语法。
I'm a little confused about your problem.
If your user control contains a DropDownList control, just inititalize somewhere in the user control.
The easiest way is in the Codebehind for the usercontrol, just do DropDownList.Items.Add() or whatever the syntax is for adding an item.
这是我用来为组合框创建自定义数据源的模板:
这就是我加载它的方式:
希望这会有所帮助。需要记住的一件事是,如果 cboNumber 有一个已分配给 SelectedIndexchanged 事件的处理程序,您将遇到问题。所以不要创建默认事件。
Here is the template I use to create a custom datasource for my comboboxes:
And this is how I load it:
Hope this helps. One thing to keep in mind is that if cboNumber has a handler already assigned to the SelectedIndexchanged event, you will encounter problems. So don't create a default event.