Xaml-dependency-properties
XAML-依存関係プロパティ
依存関係プロパティは、Windowsランタイムアプリの一部でもある鋭いプロパティシステムが値に続く特定のタイプのプロパティです。 依存関係プロパティを定義するクラスは、DependencyObjectクラスから継承する必要があります。
XAMLで使用されるUIコントロールクラスの多くは、DependencyObjectクラスから派生し、依存関係プロパティをサポートしています。 次のXAMLコードは、いくつかのプロパティを持つボタンを作成します。
<Window x:Class = "XAMLDependencyProperty.MainWindow"
xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local = "clr-namespace:XAMLDependencyProperty"
Title = "MainWindow" Height = "350" Width = "604">
<Grid>
<Button Height = "40" Width = "175" Margin = "10" Content = "Dependency Property">
<Button.Style>
<Style TargetType = "{x:Type Button}">
<Style.Triggers>
<Trigger Property = "IsMouseOver" Value = "True">
<Setter Property = "Foreground" Value = "Red"/>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</Grid>
</Window>
XAMLのx:Typeマークアップ拡張機能には、C#のtypeof()などの同様の機能があります。 <Style TargetType = "\ {x:Type Button}">などのオブジェクトのタイプをとる属性が指定されている場合に使用されます
上記のコードをコンパイルして実行すると、次のMainWindowが生成されます。 マウスがボタンの上にあるとき、ボタンの前景色を変更します。 マウスがボタンを離れると、元の色に戻ります。
依存関係プロパティと他のCLRプロパティの主な違いは-
- CLRプロパティは、 getter および setter を使用して、クラスのプライベートメンバーから直接読み書きできます。 依存関係プロパティの場合、ローカルオブジェクトには保存されません。
- 依存関係プロパティは、DependencyObjectクラスによって提供されるキー/値ペアの辞書に保存されます。
- また、変更時にプロパティを保存するため、多くのメモリを節約します。
- XAMLでもバインドできます。
- 依存関係プロパティを宣言し、システムコールレジスタで登録します。
- プロパティのセッターとゲッターを提供します。
- グローバルに発生する変更を処理する静的ハンドラーを定義します。
- インスタンスハンドラを定義して、その特定のインスタンスに発生する変更を処理します。
以下に、ユーザーコントロールのSetTextプロパティを設定するように定義された依存関係プロパティのC#のコードを示します。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication3 {
///<summary>
///Interaction logic for UserControl1.xaml
///</summary>
public partial class UserControl1 : UserControl {
public UserControl1() {
InitializeComponent();
}
public static readonly DependencyProperty
SetTextProperty = DependencyProperty.Register("SetText", typeof(string),
typeof(UserControl1), new PropertyMetadata("",
new PropertyChangedCallback(OnSetTextChanged)));
public string SetText {
get {return(string) GetValue(SetTextProperty); }
set {SetValue(SetTextProperty, value);}
}
private static void OnSetTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
UserControl1 UserControl1Control = d as UserControl1;
UserControl1Control.OnSetTextChanged(e);
}
private void OnSetTextChanged(DependencyPropertyChangedEventArgs e) {
tbTest.Text = e.NewValue.ToString();
}
}
}
TextBlockがユーザーコントロールとして定義され、SetText依存関係プロパティによってTextプロパティが割り当てられるXAMLファイルを次に示します。
次のXAMLコードは、SetText依存プロパティと他のいくつかのプロパティを初期化するユーザーコントロールを作成します。
<Window x:Class = "WpfApplication3.MainWindow"
xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:views = "clr-namespace:WpfApplication3"
Title = "MainWindow" Height = "350" Width = "604">
<Grid>
<views:UserControl1 SetText = "Hellow World"/>
</Grid>
</Window>
このアプリケーションを実行してみましょう。MainWindowで、ユーザーコントロールの依存関係プロパティがテキストとして正常に使用されていることがすぐにわかります。