首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何匹配自定义PropertyGrid BrowsableAttributes条目?

如何匹配自定义PropertyGrid BrowsableAttributes条目?
EN

Stack Overflow用户
提问于 2012-09-30 15:35:47
回答 1查看 701关注 0票数 4

我有一个具有以下代码的类(KeywordProperties):

代码语言:javascript
复制
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; }
}

另外还有一个用于检查属性的:

代码语言:javascript
复制
[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中显示属性:

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-30 16:03:18

您的代码似乎只在两个值都有一个值时才与DisplayModeAttributes匹配,或者一个值包含单个值,另一个包含多个值;如果两个值都包含多个值,则不匹配它们,除非值列表是相同的。

要按原样使用代码,可以更改填充PropertyGrid.BrowsableAttributes的方式。

代码语言:javascript
复制
propertygrid1.BrowsableAttributes = new AttributeCollection(
    new DisplayModeAttribute("0-1"),
    new DisplayModeAttribute("1-2")
    // etc.
);

或者,要修复匹配的代码,可以将其替换为以下内容:

代码语言:javascript
复制
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相交方法,它返回两个列表共有的元素。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12662496

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档