LoginSignup
1
4

More than 5 years have passed since last update.

PropertyGrid で TypeConverter と PropertyDescriptor の理解を深める

Last updated at Posted at 2019-04-04

はじめに

System.Xml.Linq.XDocument クラスの内容をプロパティグリッドに表示する。

やりたいこと

xml.png
 ↑ この XML を ↓ このように表示
pg.png

XML の階層構造をプロパティグリッドにそのまま表示するように、カスタム型コンバーターを作成してみる。

PropertyGrid について

System.Windows.Forms.PropertyGrid コントロールを使うとクラスのインスタンスを簡単に表示・編集できる。

属性一覧

表示内容は属性指定でカスタマイズ可能で、主な属性は下記

⇒ 型コンバーターを自作することでプロパティグリッドに表示する内容をカスタマイズできる。

XML について

XML は System.Xml.Linq.XDocument クラスで扱うこととする。

System.Xml.Linq
XObject
 ┣ XDeclaration 
 ┣ XNode
 ┃  ┣ XProcessingInstruction
 ┃  ┣ XDocumentType
 ┃  ┣ XContainer
 ┃  ┃  ┣ XDocument
 ┃  ┃  ┗ XElement : 要素
 ┃  ┣ XText
 ┃  ┃  ┗ XCData
 ┃  ┗ XComment
 ┗ XAttribute : 属性

[code-0] 準備

XML 要素と属性の名前などを取得する用に、XObject の拡張メソッドを用意しておく。

XObjectExtensions.cs
static class XObjectExtensions
{
    public static bool HasElements(this XObject obj) => (obj as XElement)?.HasElements ?? false;

    public static string GetName(this XObject obj)
    {
        switch (obj.NodeType)
        {
            case XmlNodeType.Element: return (obj as XElement).Name.LocalName;
            case XmlNodeType.Attribute: return "@" + (obj as XAttribute).Name.LocalName;
            default: throw new NotSupportedException();
        }
    }

    public static string GetValue(this XObject obj)
    {
        switch (obj.NodeType)
        {
            case XmlNodeType.Element: return (obj as XElement).Value;
            case XmlNodeType.Attribute: return (obj as XAttribute).Value;
            default: throw new NotSupportedException();
        }
    }

    public static void SetValue(this XObject obj, string value)
    {
        switch (obj.NodeType)
        {
            case XmlNodeType.Element: (obj as XElement).Value = value; break;
            case XmlNodeType.Attribute: (obj as XAttribute).Value = value; break;
            default: throw new NotSupportedException();
        }
    }
}

型コンバーターを作成

System.ComponentModel.TypeConverter から派生したクラスを作成することで、プロパティグリッドに表示する項目を自由に指定できる。

[code-1] XmlConverter クラス

TypeConverter クラスを継承して XmlConverter クラスを作成する。

XmlConverter.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Xml;
using System.Xml.Linq;

class XmlConverter : TypeConverter
{
    protected readonly Type type;
    public XmlConverter(Type type)
    {
        this.type = type;
    }

    //todo ここへ後述のコードを追記します
}

[code-2] XmlPropertyDescriptor クラス

SimplePropertyDescriptor クラスを継承して内部クラスを作成する。
このクラスが、プロパティグリッドの1項目に相当

XmlConverter.cs
    class XmlPropertyDescriptor : SimplePropertyDescriptor
    {
        protected readonly XObject obj;

        public XmlPropertyDescriptor(Type componentType, XObject obj, params Attribute[] attributes) : base(componentType, obj.GetName(), obj.GetType(), attributes)
        {
            this.obj = obj;
            this.Converter = this.HasElements ? this.CreateConverter(componentType) : new StringConverter();
        }

        private bool HasElements => this.obj.HasElements();

        public override TypeConverter Converter { get; }

        protected virtual TypeConverter CreateConverter(Type componentType) => new XmlConverter(componentType);

        public override object GetValue(object component)
        {
            return this.HasElements ? this.obj : (object)this.obj.GetValue();
        }

        public override void SetValue(object component, object value)
        {
            if (!this.HasElements) this.obj.SetValue((string)value);
        }
    }

[code-3] CreateDescriptors メソッド

XmlPropertyDescriptor のインスタンスを作成するメソッドを実装する。

XmlConverter.cs
    private IEnumerable<PropertyDescriptor> CreateDescriptors(XElement element)
    {
        foreach (var attribute in element.Attributes())
        {
            yield return this.CreateDescriptor(attribute); // 属性を列挙
        }
        foreach (var child in element.Elements())
        {
            yield return this.CreateDescriptor(child); // 子要素を列挙
        }
    }

    private PropertyDescriptor CreateDescriptor(XObject obj)
    {
        var category = obj.Parent.Name.LocalName;
        return new XmlPropertyDescriptor(this.type, obj, new CategoryAttribute(category));
    }

[code-4] プロパティ列挙

プロパティグリッドへの表示項目を列挙するメソッドをオーバーライドする。

XmlConverter.cs
    public override bool GetPropertiesSupported(ITypeDescriptorContext context) => true;
    public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
    {
        var element = value as XElement;
        if (element != null) return new PropertyDescriptorCollection(this.CreateDescriptors(element).ToArray());
        return base.GetProperties(context, value, attributes);
    }

型コンバーターを紐付

TypeConverterAttribute 属性で、使用するコンバーターを指定する。

using System.ComponentModel;
using System.Xml.Linq;

[TypeConverter(typeof(XmlConverter))]
class CustomXDocument : XDocument
{
}
1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4