えムナウ Blog

えムナウ の なすがまま

目次

Blog 利用状況

ニュース


follow mnow at http://twitter.com


えムナウのプログラミングのページ

INETAJ

書庫

日記カテゴリ

ギャラリ

2009年5月23日 #

依存関係プロパティ を使わない 添付プロパティ

依存関係プロパティ を使わない 添付プロパティ について聞かれましたので確認しました。

添付プロパティは通常は依存関係プロパティとして作成します。
しかし記憶場所を自分で確保すれば依存関係プロパティにする必要はないです。

using System.Collections.Generic;
using System.Windows;

namespace 添付プロパティ
{
    class AttachedPropertyTest1 : UIElement
    {
        public static readonly DependencyProperty TestTextProperty =
            DependencyProperty.RegisterAttached(
            "TestText", typeof (string), typeof (AttachedPropertyTest1));
        public static void SetTestText(DependencyObject element,string text)
        {
            element.SetValue(TestTextProperty,text);
        }
        public static string  GetTestText(DependencyObject element)
        {
            return (string) element.GetValue(TestTextProperty);
        }
    }
    class AttachedPropertyTest2 : UIElement
    {
        private static readonly Dictionary<DependencyObject, object> _list = new Dictionary<DependencyObject, object>();
        public static void SetTestText(DependencyObject element, string text)
        {
            if (_list.ContainsKey(element))
            {
                _list[element] = text;
            }
            else
            {
                _list.Add(element,text);
            }
        }
        public static string GetTestText(DependencyObject element)
        {
            if (_list.ContainsKey(element))
            {
                return (string)_list[element];
            }
            else
            {
                return null;
            }
        }
    }
}

<Window x:Class="添付プロパティ.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:添付プロパティ"
    Title="Window1" Height="300" Width="300">
    <Grid>
    <Grid.RowDefinitions>
      <RowDefinition />
      <RowDefinition />
      <RowDefinition />
    </Grid.RowDefinitions>
    <Label Grid.Row="0" local:AttachedPropertyTest1.TestText="aaaaa" Name="Label1">Text1</Label>
    <Label Grid.Row="1" local:AttachedPropertyTest2.TestText="bbbbb" Name="Label2">Text2</Label>
    <Button Grid.Row="2" Name="button1" Click="button1_Click">Button</Button>
  </Grid>
</Window>

private void button1_Click(object sender, RoutedEventArgs e)
{
    Debug.WriteLine("AttachedPropertyTest1/Label1=" + AttachedPropertyTest1.GetTestText(this.Label1));
    Debug.WriteLine("AttachedPropertyTest1/Label2=" + AttachedPropertyTest1.GetTestText(this.Label2));
    Debug.WriteLine("AttachedPropertyTest2/Label1=" + AttachedPropertyTest2.GetTestText(this.Label1));
    Debug.WriteLine("AttachedPropertyTest2/Label2=" + AttachedPropertyTest2.GetTestText(this.Label2));
}

 

AttachedPropertyTest1/Label1=aaaaa
AttachedPropertyTest1/Label2=
AttachedPropertyTest2/Label1=
AttachedPropertyTest2/Label2=bbbbb

 

しかし、以下のHELPのような依存関係プロパティのいいところは使えません。

「DependencyProperty は、型コンバータと、プロパティ値を設定するための XAML 属性構文をサポートします。この属性構文は、Style の Setter、Trigger、または Condition で Property 値を指定するときに使用します。」

Label1

 

ちなみに影響範囲はAttributeでget アクセサに設定します。

[AttachedPropertyBrowsableForChildren()]
添付プロパティの所有者が他の要素の親要素であるときに、その添付プロパティが参照可能になるように指定します。

[AttachedPropertyBrowsableForType(typeof(TextBox))]
複数の型を指定し、その中に一致する型が 1 つでも含まれる場合 (論理 OR)、このプロパティは参照可能です。型にはインターフェイスも指定できます。get アクセサにのみ適用します。

[AttachedPropertyBrowsableWhenAttributePresent()]
AttachedPropertyBrowsableWhenAttributePresentAttribute が適用されているクラスで添付プロパティを参照できるように、クラスでも適用する必要がある .

posted @ 21:13 | Feedback (2)