3

重複の可能性:
条件付きの「閲覧可能」属性

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動的に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)

エラー:

属性の引数は、定数式、typeof式、または属性パラメーターtypeの配列作成式である必要があります。

4

1 に答える 1

6

フィルタリングについては、のを変更するだけBrowsableAttributesですPropertyGrid。以下では、私は:

  • カスタム属性を定義します[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();
        }
    }
}
于 2012-08-22T19:11:06.103 に答える