How can I map my own PropertyGrid BrowsableAttributes records?

I have a class (KeywordProperties) with this code:

public class KeywordProperties { [DisplayMode("0-1,0-2,0-3,1-1,1-2,1-3,1-6,1-9,1-10,1-11,1-12,2-1,2-2,2-3,2-9,2-10,2-12,3-1,3-2,3-3,3-10,3-12,4-13,5,6")] public string Onvaan { get; set; } [DisplayMode("0-1,0-2,0-3,1-1,1-2,1-3,1-6,1-9,1-10,1-11,1-12,2-1,2-2,2-3,2-9,2-10,2-12,3-1,3-2,3-3,3-10,3-12,4-13,5,6")] public string MozooKolli { get; set; } [DisplayMode("0-10,1-10,3-10,3-12,5,6")] public string EsmeDars { get; set; } [DisplayMode("0-1,1-1,2-1,2-2,3-1,6")] public string Sokhanraan { get; set; } [DisplayMode("0-10,1-2,2-1,2-10,3-10,6")] public string Modares { get; set; } } 

And I have another validation attribute:

 [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; } } 

I want to display properties in a propertygrid using this code:

 String Code = "": KeywordProperties Kp = new KeywordProperties(); propertygrid1.SelectedObject = Kp; propertygrid1.BrowsableAttributes = new AttributeCollection(new DisplayModeAttribute(Code)); 

When the vlue code is "0-1" or "5" or ... (one value), I can see my properties. But, when you use "0-1,1-2" for the code, I do not see anything in my correct.

How can I see this data:

1- All properties having code 0-1 and code 1-2 :

Result: Onvaan, MozooKolli

2- All properties having code 0-1 or code 1-2 :

result: Onvaan, MozooKolli, Sokhanraan, Modares

+4
source share
1 answer

It looks like your code only matches DisplayModeAttributes , when both have one value or one contains one value and the other contains multiple values; it will not match them if both contain multiple values, unless the list of values ​​is identical.

To use your code as is, you can change the way PropertyGrid.BrowsableAttributes is populated:

 propertygrid1.BrowsableAttributes = new AttributeCollection( new DisplayModeAttribute("0-1"), new DisplayModeAttribute("1-2") // etc. ); 

Alternatively, to fix your corresponding code, you can replace it with something like:

 public override bool Match(object obj) { var other = obj as DisplayModeAttribute; if (other == null) return false; if (other.mode == mode) return true; string[] modes = mode.Split(','); string[] others = other.mode.Split(','); var matches = modes.Intersect(others); return matches.Count() > 0; } 

Here we use the LINQ Intersect method, which returns elements that have two lists.

+3
source

Source: https://habr.com/ru/post/1437050/


All Articles