我有一个具有以下代码的类(KeywordProperties):
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; }
}另外还有一个用于检查属性的:
[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中显示属性:
String Code = "":
KeywordProperties Kp = new KeywordProperties();
propertygrid1.SelectedObject = Kp;
propertygrid1.BrowsableAttributes = new AttributeCollection(new DisplayModeAttribute(Code));当代码vlue为"0-1“或"5”或.(单个值)时,我可以看到我的属性。但是,当使用"0-1,1-2“作为代码时,我无法在我的属性网格中看到任何东西。
我如何才能看到这些数据:
1-具有代码0-1和代码1-2的所有属性:
结果是:Onvaan,MozooKolli
2-具有代码0-1或代码1-2的所有属性:
结果是:Onvaan,MozooKolli,Sokhanraan,Modares
发布于 2012-09-30 16:03:18
您的代码似乎只在两个值都有一个值时才与DisplayModeAttributes匹配,或者一个值包含单个值,另一个包含多个值;如果两个值都包含多个值,则不匹配它们,除非值列表是相同的。
要按原样使用代码,可以更改填充PropertyGrid.BrowsableAttributes的方式。
propertygrid1.BrowsableAttributes = new AttributeCollection(
new DisplayModeAttribute("0-1"),
new DisplayModeAttribute("1-2")
// etc.
);或者,要修复匹配的代码,可以将其替换为以下内容:
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;
}这使用了LINQ相交方法,它返回两个列表共有的元素。
https://stackoverflow.com/questions/12662496
复制相似问题