【发布时间】:2015-12-29 11:04:24
【问题描述】:
我遇到了一个问题,我不知道这是否确实可行(如果有“hacky”方式,我全力以赴,但我还没有找到)。
我有一个 IExtenderProvider 组件,我用它来拥有我自己的 UITypeEditor 用于第三方控件上的某些属性(由于显而易见的原因,我无法更改)。
这些控件不一定继承自同一个基(如果继承,基不一定具有我想要扩展的属性,并且这些属性是在同一个类中定义的)。
所以,假设我想为属性Image、Glyph、LargeGlyph、SmallGlyph 在它们上创建一个替代属性。
所以我有类似的东西:
[ProvideProperty("LargeGlyphCustom", typeof (object))]
[ProvideProperty("GlyphCustom", typeof(object))]
[ProvideProperty("SmallImageCustom", typeof(object))]
[ProvideProperty("LargeImageCustom", typeof(object))]
[ProvideProperty("ImageCustom", typeof(object))]
public class MyImageExtender : Component, IExtenderProvider
{
private readonly Type[] _extendedTypes =
{
typeof (OtherControl),
typeof (SomeOtherControl),
typeof (AControl),
typeof (AButton)
};
bool IExtenderProvider.CanExtend(object o)
{
if (!DesignMode) return false;
return _extendedTypes.Any(t => t.IsInstanceOfType(o));
}
// Implement the property setter and getter methods
}
到目前为止,一切都很好。我可以在我期望的类型的控件上看到我的属性。
但是,这些是控件中属性的替换(只是为了更改UITypeEditor)。
我的方法的问题在于,我在 all 的扩展类型中看到了 all 的扩展属性。
说,如果AButton只有Image,我只想看到ImageCustom而不是SmallImageCustom、LargeImageCustom等。
所以我的方法是这样做:
[ProvideProperty("LargeGlyphCustom", typeof (OtherControl))]
// other properties
[ProvideProperty("ImageCustom", typeof(AButton))]
public class MyImageExtender : Component, IExtenderProvider
// ...
这似乎工作正常,现在我只在AButton 上看到ImageCustom,在OtherControl 上看到LargeGlyphCustom。
现在的问题是,如果我想在AButton 和OtherControl 中都显示ImageCustom,我曾想过这样做:
[ProvideProperty("ImageCustom", typeof(AButton))]
[ProvideProperty("ImageCustom", typeof(OtherControl))]
public class MyImageExtender : Component, IExtenderProvider
这不起作用,我只能在AButton 上看到ImageCustom,但在OtherControl 上看不到。
反编译ProvidePropertyAttribute 的源代码,发生这种情况的原因“可以说”很清楚。它在内部创建了一个 TypeId,我怀疑是 WinForms 设计器正在使用的东西,如下所示:
public override object TypeId
{
get
{
return (object) (this.GetType().FullName + this.propertyName);
}
}
这使得 TypeId 为"ProvidePropertyAttributeImageCustom",因此无法区分不同的接收者类型。
我将测试派生 ProvidePropertyAttribute 并创建一个不同的 TypeId,因为它似乎可以覆盖,但我希望 winforms 设计师期望特定的 ProvidePropertyAttribute 类型而不是派生类型(winforms 设计师对这些东西很挑剔)。
哎呀,ProvidePropertyAttribute 是 sealed,所以我无法派生和制作我的自定义 TypeId,看来(并不是我寄予厚望,这会起作用)
与此同时,有人做过类似的事情并且知道我可以使用的东西吗?
【问题讨论】:
-
您要扩展的控件是您的自定义控件吗?
-
@RezaAghaei 不,正如问题所述,它们是第三方(我没有源代码)
标签: c# winforms windows-forms-designer