かずきのBlog@hatena

すきな言語は C# + XAML の組み合わせ。Azure Functions も好き。最近は Go 言語勉強中。日本マイクロソフトで働いていますが、ここに書いていることは個人的なメモなので会社の公式見解ではありません。

WPF4.5入門 その8 「オブジェクト要素のプロパティ」

オブジェクト要素のプロパティ

XAMLでオブジェクト要素を使ってオブジェクトを構築することができることがわかりました。次に、オブジェクト要素のプロパティの設定方法を紹介します。オブジェクト要素のプロパティの設定方法は「属性構文」と「プロパティ要素の構文」の2通りがあります。まずは属性構文について説明します。
属性構文は、名前の通りXMLの属性としてプロパティを定義する方法です。例えば、先ほどのPersonクラスにFullNameとSalaryという2つのプロパティを以下のように追加したとします。

namespace CustomXaml
{
    using System;
 
    public class Person
    {
        public Person()
        {
            this.Birthday = DateTime.Now;
        }
 
        public DateTime Birthday { get; private set; }
 
        // 名前と給料を追加
        public string FullName { get; set; }
        public int Salary { get; set; }
    }
}

この2つのプロパティを属性構文を使って指定すると以下のようになります。

<p:Person xmlns:p="clr-namespace:CustomXaml;assembly=CustomXaml" 
    FullName="田中 太郎"
    Salary="300000"/>

XAMLを読み込んで表示するプログラムに追記をしてFullNameとSalaryも表示するように変更します。

namespace CustomXaml
{
    using System;
    using System.Windows.Markup;
 
    class Program
    {
        public static void Main(string[] args)
        {
            // アセンブリから対象のXAMLのストリームを取得
            var s = typeof(Program).Assembly.GetManifestResourceStream("CustomXaml.Person.xaml");
            // パース
            var p = XamlReader.Load(s) as Person;
            // プロパティを表示してみる
            Console.WriteLine("p.FullName = {0}, p.Salary = {1}, p.Birthday = {2}", 
                p.FullName,
                p.Salary,
                p.Birthday);
        }
    }
}

実行すると、以下のような結果が表示されます。

p.FullName = 田中 太郎, p.Salary = 300000, p.Birthday = 2013/01/03 15:53:04
続行するには何かキーを押してください . . .

もう1つの「プロパティ要素の構文」では、プロパティを特殊な命名規約に従ったタグとして記述できます。FullNameとSalaryを「プロパティ要素の構文」で設定したXAMLを以下に示します。

<p:Person xmlns:p="clr-namespace:CustomXaml;assembly=CustomXaml">
    <p:Person.FullName>
        田中 太郎
    </p:Person.FullName>
    <p:Person.Salary>
        300000
    </p:Person.Salary>
</p:Person>

このように「プロパティ要素の構文」ではプロパティを<クラス名.プロパティ名>という命名規約のタグとして指定できます。今回の例のように単純なものではメリットは出ないですが、プロパティの型がオブジェクトの場合、この記法が役に立ちます。例えばPersonクラスにFather、MotherというPerson型のプロパティがあった場合「属性構文」ではXAMLでFatherとMotherに値を設定することができません。「プロパティ要素の構文」があることで、XAMLで複雑なオブジェクトを組み立てることが出来るようになっています。
Person型のFatherとMotherを追加したPersonクラスの定義を以下に示します。

namespace CustomXaml
{
    using System;
 
    public class Person
    {
        public Person()
        {
            this.Birthday = DateTime.Now;
        }
 
        public DateTime Birthday { get; private set; }
 
        // 名前と給料を追加
        public string FullName { get; set; }
        public int Salary { get; set; }
 
        // 父親と母親
        public Person Father { get; set; }
        public Person Mother { get; set; }
    }
}

「オブジェクト要素の構文」を使ってFatherとMotherを設定しているXAMLを以下に示します。

<p:Person xmlns:p="clr-namespace:CustomXaml;assembly=CustomXaml" 
          FullName="田中 太郎" 
          Salary="300000">
    <!-- オブジェクト要素の構文でプロパティにオブジェクトを設定している例 -->
    <p:Person.Father>
        <p:Person FullName="田中 父" />
    </p:Person.Father>
    <p:Person.Mother>
        <p:Person FullName="田中 母" />
    </p:Person.Mother>
</p:Person>

このXAMLを読み込んで父親と母親の名前を表示するプログラムを以下に示します。

namespace CustomXaml
{
    using System;
    using System.Windows.Markup;
 
    class Program
    {
        public static void Main(string[] args)
        {
            // アセンブリから対象のXAMLのストリームを取得
            var s = typeof(Program).Assembly.GetManifestResourceStream("CustomXaml.Person.xaml");
            // パース
            var p = XamlReader.Load(s) as Person;
            // プロパティを表示してみる
            Console.WriteLine("FullName = {0}, Father.FullName = {1}, Mother.FullName = {2}", 
                p.FullName,
                p.Father.FullName,
                p.Mother.FullName);
        }
    }
}

実行すると、以下のようにFatherプロパティとMotherプロパティに値が設定できていることが確認できます。

FullName = 田中 太郎, Father.FullName = 田中 父, Mother.FullName = 田中 母
続行するには何かキーを押してください . . .