简体   繁体   English

重写ComboBox.Items的最佳方法

[英]Best Way to Override ComboBox.Items

I've created an owner-draw user control that inherits from ComboBox. 我创建了一个继承自ComboBox的所有者绘制用户控件。

The control stores specialized items, but the Items collection still accepts and returns items of type Object. 该控件存储特殊项目,但是Items集合仍接受并返回Object类型的项目。 Any tips on the best way to override this collection to be type-safe? 关于覆盖此集合以确保其类型安全的最佳方法的任何技巧?

About the only way I can think of is to create my own collection class. 我能想到的唯一方法是创建自己的集合类。 The class wouldn't be a true collection--it would take an ObjectCollection as an argument to the constructor and simply extend the methods of that to. 该类将不是一个真正的集合,它将一个ObjectCollection作为构造函数的参数,并简单地将其方法扩展到。

The user control would pass the original Items collection to the constructor of the new class. 用户控件会将原始的Items集合传递给新类的构造函数。 And then override the Items property to return an instance of the new class instead. 然后重写Items属性以返回新类的实例。

This seems somewhat convoluted. 这似乎有些令人费解。 Is there a better way? 有没有更好的办法?

You could do it a couple of different ways. 您可以通过几种不同的方式来做到这一点。 First off, I'm a little confused about you creating a "user control" that inherits from ComboBox. 首先,我对您创建一个从ComboBox继承的“用户控件”感到有些困惑。 You can create a custom control that inherits from ComboBox, but you cannot inherit from both UserControl (which is the usual definition of a "user control") and ComboBox. 您可以创建从ComboBox继承的自定义控件 ,但是不能同时从UserControl(这是“用户控件”的通常定义)和ComboBox继承。

If you define a true UserControl derivative, this gets a little easier. 如果您定义了一个真正的UserControl派生类,这会变得容易一些。 A UserControl, like I said, can't be a ComboBox, but it can have a ComboBox. 就像我说的,UserControl不能 ComboBox,但是可以具有 ComboBox。 So, you can drop and Dock a ComboBox to your UserControl surface, and then re-implement any properties you need to be able to use, such as Items. 因此,您可以将ComboBox拖放到UserControl曲面上,然后重新实现需要使用的所有属性,例如Items。 This will allow you to re-create Items as a collection of your choice, probably a strongly-typed List. 这将使您可以重新创建项目作为您选择的集合,可能是强类型的列表。 The only problem will be knowing exactly what you'll want to re-implement; 唯一的问题是确切知道要重新实现的内容。 there's a lot of useful properties of a ComboBox that you won't be able to access in the Designer or in code unless you implement a "pass-through" property that modified the contained ComboBox within your UserControl. ComboBox有很多有用的属性,除非您实现了一个“传递”属性,该属性可以修改UserControl中包含的ComboBox,否则您将无法在Designer或代码中访问它。

If you inherit directly from ComboBox, it gets a little tricker in some ways, easier in others. 如果直接从ComboBox继承,则它在某些方面会变得有些欺骗,而在其他方面则更容易。 You can hide the base class implementation of Items by defining your own and using the new keyword. 您可以通过定义自己的项目并使用new关键字来隐藏Items的基类实现。 You can change the visibility, type and other modifiers when you do this. 您可以在执行此操作时更改可见性,类型和其他修饰符。 This will prevent code that deals with your control as a CustomComboBox (or whatever you name it) from using the object array on the base class; 这将防止与作为CustomComboBox(或您命名的控件)的控件一起处理的代码使用基类上的对象数组。 they have to use your strongly-typed Items array. 他们必须使用您的强类型Items数组。 Your new property can still access the old one (which it will need to in order to make it work). 您的新媒体资源仍可以访问旧媒体资源(为了使其正常工作,需要访问该媒体资源)。 You also get all the other public properties of a ComboBox for free; 您还可以免费获得ComboBox的所有其他公共属性; you only have to reimplement what you want to change. 您只需重新实现要更改的内容。 However, referring to your custom ComboBox as any base class will cause the runtime to use the version of Items that is valid for that class; 但是,将您的自定义ComboBox称为任何基类将导致运行时使用对该类有效的Items版本。 that is, the object array, not your strongly-typed one. 即对象数组,而不是您的强类型数组。

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

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