2012-08-22 86 views
3

Possible Duplicate:
Conditional 「Browsable」 Attribute如何在propertygrid c#中的屬性中設置BrowseAttribute true或false?

我定義了AppSettings類,它有幾個屬性。在我的表格中,當我,我想要顯示屬性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) 

錯誤:

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

+0

對於PropertyGrid中,ID使用的TypeConverter - 平凡然後 –

回答

6

用於過濾,我只想改變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(); 
     } 
    } 
} 
+0

非常好,非常好,非常好,感謝的。 –

+0

非常聰明,馬克。做得很好! – MrWuf

相關問題