C# 枚举类型的 UserControl 属性在设计器中显示为 bool 或根本不显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/64139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 10:23:05  来源:igfitidea点击:

UserControl Property of type Enum displays in designer as bool or not at all

提问by Adam Haile

I have a usercontrol that has several public properties. These properties automatically show up in the properties window of the VS2005 designer under the "Misc" category. Except two of the properties which are enumerations don't show up correctly.

我有一个具有多个公共属性的用户控件。这些属性会自动显示在 VS2005 设计器的“其他”类别下的属性窗口中。除了作为枚举的两个属性没有正确显示。

The first on uses the following enum:

第一个使用以下枚举:

public enum VerticalControlAlign
{
    Center,
    Top,
    Bottom
}

This does not show up in the designer at all.

这根本没有出现在设计器中。

The second uses this enum:

第二个使用这个枚举:

public enum AutoSizeMode
{
    None,
    KeepInControl
}

This one shows up, but the designer seems to think it's a bool and only shows True and False. And when you build a project using the controls it will say that it can't convert type bool to AutoSizeMode.

这个出现了,但设计师似乎认为它是一个bool,只显示True和False。当您使用控件构建项目时,它会说它无法将类型 bool 转换为 AutoSizeMode。

Also, these enums are declared globably to the Namespace, so they are accessible everywhere.

此外,这些枚举被全局声明到命名空间,因此它们在任何地方都可以访问。

Any ideas?

有任何想法吗?

采纳答案by Mark Ingram

For starters, the second enum, AutoSizeMode is declared in System.Windows.Forms. So that might cause the designer some issues.

首先,第二个枚举 AutoSizeMode 在 System.Windows.Forms 中声明。所以这可能会给设计者带来一些问题。

Secondly, you might find the following page on MSDN useful:

其次,您可能会发现 MSDN 上的以下页面很有用:

http://msdn.microsoft.com/en-us/library/tk67c2t8.aspx

http://msdn.microsoft.com/en-us/library/tk67c2t8.aspx

回答by Thunder3

Some things to try (designer mode in VS2005 I have found to be somewhat flaky):

一些尝试(我发现 VS2005 中的设计器模式有点不稳定):

  1. Open your web.config and add: batch="false"to your <compilation>tag.
  2. Try setting defaults to your enums:

    public enum VerticalControlAlign
    {
        Center = 0,
        Top = 1,
        Bottom = 2
    }
    
  1. 打开您的 web.config 并添加:batch="false"到您的<compilation>标签。
  2. 尝试将默认值设置为您的枚举:

    public enum VerticalControlAlign
    {
        Center = 0,
        Top = 1,
        Bottom = 2
    }
    

回答by Craig Eddy

You do not need to make your enums global in order for them to be visible in the designer.

您不需要将枚举设为全局,以便它们在设计器中可见。

Clarify please:

请澄清:

  1. if you add another value to your AutoSizeMode enum, does it still appear as a boolean?
  2. If (instead) you change the name of enum, does it still appear as a boolean?
  1. 如果向 AutoSizeMode 枚举添加另一个值,它是否仍显示为布尔值?
  2. 如果(相反)您更改枚举的名称,它是否仍显示为布尔值?

回答by Statement

I made a little test with your problem (I'm not sure if I understood it correctly), and these properties shows up in the designer correctly, and all enums are shown appropriately. If this isn't what you're looking for, then please explain yourself further.

我对您的问题进行了一些测试(我不确定我是否理解正确),并且这些属性正确显示在设计器中,并且所有枚举都正确显示。如果这不是您要查找的内容,请进一步解释一下。

Don't get hang up on the _Ugly part thrown in there. I just used it for a quick test.

不要挂断其中的_Ugly 部分。我只是用它来进行快速测试。

using System.ComponentModel;
using System.Windows.Forms;

namespace SampleApplication
{
    public partial class CustomUserControl : UserControl
    {
        public CustomUserControl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// We're hiding AutoSizeMode in UserControl here.
        /// </summary>
        public new enum AutoSizeMode { None, KeepInControl }
        public enum VerticalControlAlign { Center, Top, Bottom }

        /// <summary>
        /// Note that you cannot have a property  
        /// called VerticalControlAlign if it is   
        /// already defined in the scope.
        /// </summary>
        [DisplayName("VerticalControlAlign")]
        [Category("stackoverflow.com")]
        [Description("Sets the vertical control align")]
        public VerticalControlAlign VerticalControlAlign_Ugly
        {
            get { return m_align; }
            set { m_align = value; }
        }
        private VerticalControlAlign m_align;        

        /// <summary>
        /// Note that you cannot have a property  
        /// called AutoSizeMode if it is   
        /// already defined in the scope.
        /// </summary>
        [DisplayName("AutoSizeMode")]
        [Category("stackoverflow.com")]
        [Description("Sets the auto size mode")]
        public AutoSizeMode AutoSizeMode_Ugly
        {
            get { return m_autoSize; }
            set { m_autoSize = value; }
        }
        private AutoSizeMode m_autoSize;    
    }
}