【问题标题】:Why are certain control properties shown in the Visual Studio designer, and others not?为什么某些控件属性显示在 Visual Studio 设计器中,而其他控件属性不显示?
【发布时间】:2011-06-14 21:18:08
【问题描述】:

获取我的 navigationItem 用户控件:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Uboldi.Helpers;

namespace Uboldi
{
    public partial class NavigationItem : UserControl
    {
        public bool IsSelected { get; set; }
        public string Text { get; set; }

        public NavigationItem()
        {
            InitializeComponent();
            RefreshDisplay();
        }

        private void RefreshDisplay()
        {
            if (IsSelected)
                this.BackColor = CustomizationHelper.GetSecondaryColor();
            else
                this.BackColor = CustomizationHelper.GetPrimaryColor();            
        }
    }
}

在 Visual Studio 中,我可以看到 IsSelected 属性,但看不到 Text 属性。

有什么原因吗?

【问题讨论】:

  • 正确的术语是“属性”而不是属性。

标签: c# visual-studio designer propertyeditor


【解决方案1】:

Text 属性继承自 UserControl。在隐藏的地方,用户控件没有任何有意义的方式来显示文本。您必须再次继承它并关闭所有使其隐藏的属性。像这样:

    [Browsable(true)]
    [EditorBrowsable(EditorBrowsableState.Always)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [Bindable(true)]
    public override string Text {
        get { return base.Text; }
        set { base.Text = value; }
    }

【讨论】:

【解决方案2】:

您需要使用BrowsableAttribute 标记您希望在设计时属性列表中可见的属性。

[Browsable(true)]
public bool Text { get; set; } 

猜测,IsSelected 属性是继承的,并且设置了此属性。我可能会离开,因为我认为编译器会警告您,如果是这种情况,您正在遮蔽继承的属性。

【讨论】:

  • 为什么会这样说:“警告:您必须重建您的项目才能使 Uboldi.LeftNavigationbar 的更改显示在任何打开的设计器中。”当我重建时,更改会丢失?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-16
  • 1970-01-01
相关资源
最近更新 更多