自定义控件不会在设计器中公开图像属性

发布于 2024-11-06 10:27:49 字数 1496 浏览 1 评论 0原文

我制作了一个自定义轨迹栏控件,主要作为练习。我知道我可以/应该继承我需要的东西,而不是重新发明轮子,但在我的努力过程中我学到了很多东西。 现在,我有很多属性,除了几个图像属性之外,所有属性都显示在设计器中。这就是我所拥有的,以其他工作属性为模型(那些是整数和颜色等等,它们都按预期工作......),所以也许我应该以其他方式处理图像。最重要的是,我不知道我在做什么:)

编辑: 我的自定义控件位于 Windows 窗体解决方案 (VC# 2008 Express) 中,需要澄清的是,我的问题是某些我的控件的属性(图像属性)在设计时未显示在属性选项卡中。

编辑2:在阅读了 DependencyProperty 后,完全无法理解这个概念(我对 codez 编程或者你们大师所说的黑色巫毒魔法非常菜鸟??)。我已经习惯让 IDE 解决我所有的麻烦,并且我很高兴看到 IDE 愉快地显示我的其他属性,例如 Color BarColorint 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黯然 2024-11-13 10:27:49

我在这里找到了一些东西:
http://msdn.microsoft.com/en-us /library/system.drawing.design.imageeditor.aspx

认为这个问题已解决。

傻比既视感 2024-11-13 10:27:49

对于在设计器中显示图像,您需要重写绘制它的 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.

沉溺在你眼里的海 2024-11-13 10:27:49

您的示例没有显示您的问题到底是什么,但如果您创建自定义控件并希望从设计器访问其属性,您应该像这样使用:

        public static readonly DependencyProperty ValueProperty =
        DependencyProperty.Register("Value", typeof(object), typeof(CustomControl),
        new FrameworkPropertyMetadata(null, ValueChanged));

希望这对您有帮助

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:

        public static readonly DependencyProperty ValueProperty =
        DependencyProperty.Register("Value", typeof(object), typeof(CustomControl),
        new FrameworkPropertyMetadata(null, ValueChanged));

hope this helps you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文