自定义控件不会在设计器中公开图像属性
我制作了一个自定义轨迹栏控件,主要作为练习。我知道我可以/应该继承我需要的东西,而不是重新发明轮子,但在我的努力过程中我学到了很多东西。 现在,我有很多属性,除了几个图像属性之外,所有属性都显示在设计器中。这就是我所拥有的,以其他工作属性为模型(那些是整数和颜色等等,它们都按预期工作......),所以也许我应该以其他方式处理图像。最重要的是,我不知道我在做什么:)
编辑: 我的自定义控件位于 Windows 窗体解决方案 (VC# 2008 Express) 中,需要澄清的是,我的问题是某些我的控件的属性(图像属性)在设计时未显示在属性选项卡中。
编辑2:在阅读了 DependencyProperty 后,完全无法理解这个概念(我对 codez 编程或者你们大师所说的黑色巫毒魔法非常菜鸟??)。我已经习惯让 IDE 解决我所有的麻烦,并且我很高兴看到 IDE 愉快地显示我的其他属性,例如 Color BarColor
、int Value
等。为什么 Image LeftImage
会有所不同,很多标准控件都具有 Image 属性,也许我认为 IDE 可以解决我所有的错误是天真的,但肯定是这些人微软并没有每次都构建一个新的编辑器必须在其控件中设置图像属性。我敢打赌他们会重复使用一些东西,我也应该能够做到。
我被卡住了 :(
无论如何,这是我蹩脚的 CoD3Z:
private Image _LeftImage;
/// <summary>
/// Sets the small image appearing to the left of the trackbar
/// </summary>
[Description("The small image appearing to the left of the trackbar"),
Category("Appearance"),
DefaultValue(typeof(Image),"null"),
Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
public Image LeftImage
{
private get { return _LeftImage; }
set
{
if (value.Height != 16 || value.Width != 16)
{
_LeftImage = new Bitmap(value,new Size(16,16));
}
else _LeftImage = value;
Invalidate();
}
}
顺便说一下,DefaultValue 和 DefaultValueAttribute 之间有什么区别?
感谢您的任何指示和帮助! /米凯尔
I have made a custom trackbar control, mostly as an exercise. I know i could/should have just inherited what i needed instead of reinventing the wheel, but i have learned a lot during my endeavour.
Now, i have a lot of properties, and all of them shows up in the designer apart from a couple of image properties. This is what i have, modelled on the other working properties (those are ints and Colors and what not, and they all work as expected...), so maybe i should be doing Images in some other way. Bottom line, i don't know what i'm doing :)
EDIT : My custom control is in a Windows Forms solution (VC# 2008 Express), and to clarify, my problem is that some of my control's properties (the Image properties) isn't showing in the properties-tab during design-time.
EDIT 2 : After reading up on DependencyProperty and totally failed to understand that concept (I'm very noob at programmin teh codez or what you gurus call this black voodoo magic??). I have gotten used to letting the IDE fix all my troubles, and i was pleased to see the IDE happily showing my other properties, such as Color BarColor
, int Value
etc. etc. Why would Image LeftImage
be any different, a lot of the standard controls have Image properties and maybe it's naïve of me to think that the IDE can figure all of my mistakes out, but surely the guys at Microsoft did not construct a new editor every time they had to set an image property in their controls. My bet is that they reused something, which i should be able to do as well.
I'm stuck :(
Here's my crappy CoD3Z anyway:
private Image _LeftImage;
/// <summary>
/// Sets the small image appearing to the left of the trackbar
/// </summary>
[Description("The small image appearing to the left of the trackbar"),
Category("Appearance"),
DefaultValue(typeof(Image),"null"),
Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
public Image LeftImage
{
private get { return _LeftImage; }
set
{
if (value.Height != 16 || value.Width != 16)
{
_LeftImage = new Bitmap(value,new Size(16,16));
}
else _LeftImage = value;
Invalidate();
}
}
By the way, what is the difference between DefaultValue and DefaultValueAttribute?
Thanks for any pointers and help!
/Mikael
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在这里找到了一些东西:
http://msdn.microsoft.com/en-us /library/system.drawing.design.imageeditor.aspx
认为这个问题已解决。
I have found something here:
http://msdn.microsoft.com/en-us/library/system.drawing.design.imageeditor.aspx
Consider this solved.
对于在设计器中显示图像,您需要重写绘制它的 OnPaint 方法。
DefaultValue 与 DefaultValueAttribute 相同。 .NET 中的所有属性都是继承自 System.Attribute 的类,所有这些类都类似于 Name*Attribute*,其中 Attribute 是固定的。那么“DefaultValue”就像一个别名。
For Show the Image in the Designer you need to overrides the OnPaint Method drawing it.
DefaultValue is the same that DefaultValueAttribute. All Attributes in .NET are clases that inherits from System.Attribute, and all those class are something like Name*Attribute* where Attribute is fixed. Then "DefaultValue" is like an Alias.
您的示例没有显示您的问题到底是什么,但如果您创建自定义控件并希望从设计器访问其属性,您应该像这样使用:
希望这对您有帮助
Your example doesn't show what exactly is your problem but if you create custom control and want to access its properties from designer you should use smth like that:
hope this helps you