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中.

在.NET框架中,还可以定义自定义依赖项属性.以下是在C#中定义自定义依赖项属性的步骤.

  • 使用系统调用寄存器声明并注册您的依赖项属性./p>

  • 为属性提供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();
      }
   }
}

这是XAML文件,其中TextBlock被定义为用户控件,将通过SetText依赖项属性为其分配Text属性.

以下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