C# 如何匹配自定义属性Grid BrowsableAttributes条目?
我有一个包含以下代码的类(关键字属性):C# 如何匹配自定义属性Grid BrowsableAttributes条目?,c#,C#,我有一个包含以下代码的类(关键字属性): 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; } [DisplayMod
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; }
}
String Code = "":
KeywordProperties Kp = new KeywordProperties();
propertygrid1.SelectedObject = Kp;
propertygrid1.BrowsableAttributes = new AttributeCollection(new DisplayModeAttribute(Code));
我还有另一个检查属性:
[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;
}
}
我希望使用以下代码在propertygrid中显示属性:
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; }
}
String Code = "":
KeywordProperties Kp = new KeywordProperties();
propertygrid1.SelectedObject = Kp;
propertygrid1.BrowsableAttributes = new AttributeCollection(new DisplayModeAttribute(Code));
当代码vlue为“0-1”或“5”或…(单个值)时,我可以看到我的属性。
但是,当代码使用“0-1,1-2”时,我在PropertyGrid中看不到任何东西
如何查看这些数据:
1-所有具有代码0-1和代码1-2的属性:
结果是:Onvaan,MozooKolli
2-所有具有代码0-1或代码1-2的属性:
结果是:Onvaan、MozooKolli、Sokhanraan、Modares当两者都有一个值,或者一个包含一个值,而另一个包含多个值时,您的代码似乎只匹配
DisplayModeAttributes
;当两者都包含多个值时,它将不匹配,除非值列表相同
要按原样使用代码,可以更改填充PropertyGrid.BrowsableAttributes的方式:
或者,要修复匹配的代码,您可以将其替换为以下内容:
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;
}
这使用了返回两个列表共有的元素的方法。Windows应用程序中,Propertygrid是控件。您在做什么?WebApplication或WindowsApplication或WPFAApplication?答案的第1部分有错误:“System.ComponentModel.AttributeCollection”不包含“Add”的定义!请帮帮我。啊,对不起,没有看到AttributeCollection有一个构造函数,它接受
参数
数组。我已经更正了我的示例代码。谢谢,但如何在参数列表中设置字符串数组(代码数组)。例如:propertygrid1.BrowsableAttributes=newAttributeCollection(newDisplayModeAttribute(myStringGarrayCode));我的数组包括“0-1”、“1-2”、“3-5”,。。。长度是动态的。您的DisplayModeAttribute类不支持使用这样的数组。您可以更改类来执行此操作,但也必须更改匹配的代码。或者,您可以尝试类似于propertygrid1.BrowsableAttributes=newattributeCollection(myStringArrayCode.Select(s=>newdisplaymodeAttribute)).ToArray()的方法代码>