在WPF应用程序中,依赖项属性是一种特定类型的属性,它扩展了CLR属性.它利用了WPF属性系统中可用的特定功能.
定义依赖项属性的类必须从 DependencyObject 类继承.在XAML中使用的许多UI控件类都是从 DependencyObject 类派生的,它们支持依赖属性,例如: Button类支持 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中.
在.NET框架中,也可以定义自定义依赖项属性.按照下面给出的步骤在C#中定义自定义依赖项属性.
声明并注册依赖属性使用系统调用寄存器.
为属性提供 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中,用户控件的依赖项属性已成功用作文本.