4

私はこのコードを持つクラス(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));

コード値が「0-1」または「5」または...(単一値)の場合、プロパティを確認できます。しかし、コードに「0-1,1-2」を使用すると、properygridに何も表示されません。

これらのデータを確認するにはどうすればよいですか。

1-コード0-1およびコード1-2を持つすべてのプロパティ:

結果は:Onvaan、MozooKolli

2-コード0-1またはコード1-2を持つすべてのプロパティ:

結果は:Onvaan、MozooKolli、Sokhanraan、Modares

4

1 に答える 1

3

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

これは、2 つのリストに共通する要素を返すLINQ Intersectメソッドを使用します。

于 2012-09-30T16:03:18.367 に答える