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。當滑鼠懸停在按鈕上時,它將更改按鈕的前景色。當滑鼠離開按鈕時,它將恢復到其原始顏色。

Dependency Property

依賴屬性與其他 CLR 屬性之間的主要區別在於 -

  • CLR 屬性可以透過使用 **getter** 和 **setter** 直接讀取/寫入類的私有成員。對於依賴屬性,它不會儲存在本地物件中。

  • 依賴屬性儲存在 DependencyObject 類提供的鍵/值對字典中。

  • 它還節省了大量記憶體,因為它只在屬性更改時儲存它。

  • 它也可以在 XAML 中繫結。

在 .NET 框架中,也可以定義自定義依賴屬性。以下是使用 C# 定義自定義依賴屬性的步驟。

  • 使用系統呼叫 register 宣告並註冊您的依賴屬性。

  • 為屬性提供 setter 和 getter。

  • 定義一個靜態處理程式來處理全域性發生的任何更改。

  • 定義一個例項處理程式來處理對該特定例項發生的任何更改。

下面是 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 作為使用者控制元件的 XAML 檔案,並且 Text 屬性將透過 SetText 依賴屬性分配給它。

以下 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 中立即看到使用者控制元件的依賴屬性已成功用作文字。

Hello World Example
廣告

© . All rights reserved.