Wpf-dependency-properties

提供:Dev Guides
移動先:案内検索

WPF-依存関係プロパティ

WPFアプリケーションでは、依存関係プロパティはCLRプロパティを拡張する特定のタイプのプロパティです。 WPFプロパティシステムで利用可能な特定の機能を利用します。

依存関係プロパティを定義するクラスは、 DependencyObject クラスから継承する必要があります。 XAMLで使用されるUIコントロールクラスの多くは、 DependencyObject クラスから派生しており、依存関係プロパティをサポートしています。 ボタンクラスは、 IsMouseOver 依存プロパティをサポートします。

次のXAMLコードは、いくつかのプロパティを持つボタンを作成します。

<Window x:Class = "WPFDependencyProperty.MainWindow"
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:local = "clr-namespace:WPFDependencyProperty"
   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プロパティを使用しても得られない多くの機能を提供します。

依存関係プロパティ*と他の CLRプロパティ*の主な違いは以下のとおりです-

  • CLRプロパティは、 getter および setter を使用して、クラスのプライベートメンバーから直接読み書きできます。 対照的に、依存関係プロパティはローカルオブジェクトに保存されません。
  • 依存関係プロパティは、DependencyObjectクラスによって提供されるキー/値ペアの辞書に保存されます。 また、変更時にプロパティを保存するため、多くのメモリを節約します。 XAMLでもバインドできます。

カスタム依存関係プロパティ

  • システムコールregisterで* dependencyプロパティ*を宣言して登録します。
  • プロパティの setter および getter を指定します。
  • グローバルに発生する変更を処理する* staticハンドラ*を定義します
  • 特定のインスタンスに発生する変更を処理する*インスタンスハンドラ*を定義します。

次のC#コードは、依存関係プロパティを定義して、ユーザーコントロールの SetText プロパティを設定します。

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で、ユーザーコントロールの依存関係プロパティがテキストとして正常に使用されたことをすぐに確認できます。

ユーザーの依存関係プロパティ