c# - 如何在 propertygrid c# 的属性中设置 BrowseAttribute true 或 false?

标签 c# propertygrid

<分区>

Possible Duplicate:
Conditional “Browsable” Attribute

我定义了具有一些属性的 AppSettings 类。在我的表单中,当我单击Button1时,我想显示属性1和2(1,2是显示,其他属性隐藏或不显示),当单击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 更改为 true 或 false。我该怎么做?

表单代码是:

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)

错误:

An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type

最佳答案

对于过滤,我只需更改 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();
        }
    }
}

关于c# - 如何在 propertygrid c# 的属性中设置 BrowseAttribute true 或 false?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12077294/

相关文章:

.net - 如何允许在 WinForm PropertyGrid 中编辑 “flag” 枚举?

c# - 订阅 ViewModel 中当前项目的 PropertyChanged

c# - C# 中的除法问题

c# - Entity Framework 线程安全

c# - C# 中错误的字符串排序

c++ - 使用 CMFCPropertyGrid 在可停靠 Pane 上捕获失去焦点事件

c# - 从字面上打印带有前导 0 的整数

c# - 如何将可编辑的组合框添加到 System.Windows.Forms.PropertyGrid?

c# - 如何重置 PropertyGrid 中组件属性的默认值?

c# - 在 PropertyGrid 中实现子字段