【问题标题】:How can I set BrowseAttribute true or false in properties in propertygrid c#? [duplicate]如何在 propertygrid c# 的属性中设置 BrowseAttribute true 或 false? [复制]
【发布时间】:2012-08-23 03:11:06
【问题描述】:

可能重复:
Conditional “Browsable” Attribute

我定义了具有一些属性的AppSettings 类。在我的表单中,当我click Button1 时,我想显示属性 1 和 2(1,2 显示,其他属性隐藏或不显示),当click Button2 时,我想显示属性 2 和 3(1 隐藏, 2,3是显示,其他属性是隐藏还是不显示),怎么办?

public class AppSettings
{
    [BrowsableAttribute(true), CategoryAttribute("Document Settings"), DefaultValueAttribute(true)]
    public bool SaveOnClose{ get; set; }

    [BrowsableAttribute(true), CategoryAttribute("Global Settings"), ReadOnlyAttribute(true), DefaultValueAttribute("Welcome to AppDev!")]
    public string GreetingText { get; set; }

    [BrowsableAttribute(true), DescriptionAttribute("The rate in milliseconds that the text will repeat."), CategoryAttribute("Global Settings"), DefaultValueAttribute(10)]
    public int MaxRepeatRate { get; set; }

    [BrowsableAttribute(true), CategoryAttribute("Global Settings"), DefaultValueAttribute(4)]
    public int ItemsInMRUList { get; set; }

    [BrowsableAttribute(true), DefaultValueAttribute(false)]
    public bool SettingsChanged { get; set; }

    [BrowsableAttribute(true), CategoryAttribute("Version"), DefaultValueAttribute("1.0"), ReadOnlyAttribute(true)]
    public string AppVersion { get; set; }
}

我想动态地将BrowseAttribute 更改为真或假。我该怎么做?

表格代码是:

AppSettings AppSet = new AppSettings();

AppSet.AppVersion = "2.3";
AppSet.SaveOnClose = true;
AppSet.GreetingText = "Welcome to Dar!";
AppSet.ItemsInMRUList = 4;
AppSet.MaxRepeatRate = 10;
AppSet.SettingsChanged = false;

...

propertyGrid1.SelectedObject = AppSet;

此更改有错误:

public static bool state = true;
BrowsableAttribute(state)

错误:

属性参数必须是常量表达式,typeof 表达式 或属性参数类型的数组创建表达式

【问题讨论】:

  • 对于 PropertyGrid,id 使用 TypeConverter - 然后很简单

标签: c# propertygrid


【解决方案1】:

对于过滤,我只需更改PropertyGridBrowsableAttributes。在下文中,我:

  • 定义一个自定义属性[DisplayMode(...)],它描述了什么时候应该可见
    • 覆盖 IsMatch 以指示何时应将属性视为等效
  • 用属性修饰你的类型AppSettings的一些设置
  • 更改网格上的BrowsableAttributes,指定特定的DisplayModeAttribute,然后显示
  • 启用不同的集合重复

代码如下:

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

[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class DisplayModeAttribute : Attribute
{
    private readonly string mode;
    public DisplayModeAttribute(string mode)
    {
        this.mode = mode ?? "";
    }
    public override bool Match(object obj)
    {
        var other = obj as DisplayModeAttribute;
        if (other == null) return false;

        if (other.mode == mode) return true;

        // allow for a comma-separated match, in either direction
        if (mode.IndexOf(',') >= 0)
        {
            string[] tokens = mode.Split(',');
            if (Array.IndexOf(tokens, other.mode) >= 0) return true;
        }
        else if (other.mode.IndexOf(',') >= 0)
        {
            string[] tokens = other.mode.Split(',');
            if (Array.IndexOf(tokens, mode) >= 0) return true;
        }
        return false;
    }
}

public class AppSettings
{
    [DisplayMode("A"), CategoryAttribute("Document Settings"), DefaultValueAttribute(true)]
    public bool SaveOnClose { get; set; }

    [DisplayMode("A,B")]
    [CategoryAttribute("Global Settings"), ReadOnlyAttribute(true), DefaultValueAttribute("Welcome to AppDev!")]
    public string GreetingText { get; set; }

    [DisplayMode("B"), BrowsableAttribute(true), DescriptionAttribute("The rate in milliseconds that the text will repeat."), CategoryAttribute("Global Settings"), DefaultValueAttribute(10)]
    public int MaxRepeatRate { get; set; }

    [BrowsableAttribute(true), CategoryAttribute("Global Settings"), DefaultValueAttribute(4)]
    public int ItemsInMRUList { get; set; }

    [BrowsableAttribute(true), DefaultValueAttribute(false)]
    public bool SettingsChanged { get; set; }

    [BrowsableAttribute(true), CategoryAttribute("Version"), DefaultValueAttribute("1.0"), ReadOnlyAttribute(true)]
    public string AppVersion { get; set; }
}
static class Program
{
    [STAThread]
    static void Main()
    {
        using (var form = new Form())
        using (var grid = new PropertyGrid())
        {
            grid.Dock = DockStyle.Fill;
            grid.SelectedObject = new AppSettings();
            grid.BrowsableAttributes = new AttributeCollection(
                new DisplayModeAttribute("A"));
            form.Controls.Add(grid);
            form.ShowDialog();

            grid.BrowsableAttributes = new AttributeCollection(
                new DisplayModeAttribute("B"));
            form.ShowDialog();
        }
    }
}

【讨论】:

  • 很好,很好,很好,谢谢。
  • 非常聪明,马克。做得很好!
猜你喜欢
  • 2019-11-21
  • 1970-01-01
  • 2011-04-19
  • 1970-01-01
  • 1970-01-01
  • 2021-09-28
  • 1970-01-01
  • 2020-07-27
  • 1970-01-01
相关资源
最近更新 更多