是否可以动态设置属性的[DesignerSerializationVisibility]属性?

时间:2018-01-31 14:05:32

标签: c# winforms visual-studio-2013

我正在使用Visual Studio 2013和 我有以下枚举和属性:

public enum MoreButtonIcon
{
    Default,
    Calendar,
    Clock
}

[DefaultValue(false)]
public bool UseCustomButtonIcon
{
    get;
    set;
}

public MoreButtonIcon CustomButtonIcon
{
    get;
    set;
}

我想要属性" CustomButtonIcon "仅在" UseCustomButtonIcon "时出现在设计师中是真的。否则" CustomButtonIcon "应隐藏,或至少禁用。

是否可以设置:     [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] 属性动态属性?

2 个答案:

答案 0 :(得分:0)

我假设您正在制作自定义控件。就是这样,你可以动态地,或者换句话说,在设计师术语中 - 运行时。但你可以做的是添加一个CheckButtonIcon属性检查:

    enum MoreButtonIcon
    {
        Default,
        Calendar,
        Clock,
        UNKNOWN
    }

    private MoreButtonIcon _customButtonIcon = MoreButtonIcon.UNKNOWN;
    public MoreButtonIcon CustomButtonIcon
    {
        get => _customButtonIcon;
        set
        {
            _customButtonIcon = UseCustomButtonIcon ? value : MoreButtonIcon.UNKNOWN;
            if (DesignMode) Refresh();
        }
    }

答案 1 :(得分:0)

不可能,但您可以在属性中添加验证:

private MoreButtonIcon customButtonIcon;
public MoreButtonIcon CustomButtonIcon
{
  get { return this.customButtonIcon; }
  set
  {
    if(!this.UseCustomButtonIcon)
      throw new Exception("UseCustomButtonIcon is false!");
    this.customButtonIcon = value;
  }
}