在WPF中,微软推出了“依赖属性”这个新概念。依赖属性就是一种可以自己没有值,并能通过使用Binding从数据源获得值的属性。拥有依赖属性的对象被称为“依赖属性”。与传统的CLR属性相比它的新颖之处包括:
1、节省实例对内存的开销。
2、属性值可以通过Binding依赖在其他对象上。
依赖属性对内存的使用方式
在传统的.NET开发中,一个对象所占用的内存空间在调用new操作符进行实例化的时候就已经决定了,而WPF允许对象在被创建的时候并不包含用于存储数据的空间(即字段所占用的空间)、只保留在需要用到数据时能够获得默认值、借用其他对象数据或实时分配空间的能力,这种对象就称为依赖对象(Dependency Object)而它这种实时获取数据的能力则依靠依赖属性(Dependency Property)来实现。
使用依赖属性的例子
前端代码如下:
<Window x:Class="Learnning_DependencyProperty.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Learnning_DependencyProperty"
mc:Ignorable="d"
Title="DependencyProperty" Height="450" Width="800">
<Grid>
<StackPanel>
<TextBox x:Name="textBox1" BorderBrush="Black" Margin="5"></TextBox>
<TextBox x:Name="textBox2" BorderBrush="Black" Margin="5"></TextBox>
<Button Content="OK" Margin="5" Click="Button_Click"></Button>
</StackPanel>
</Grid>
</Window>
运行效果,如下图所示:

DependencyProperty必须以DependencyObject为宿主、借助它的SetValue和GetValue方法进行写入与读取。想使用自定义的DependencyProperty,宿主一定是DependencyObject的派生类。
xaml.cs代码如下:
public partial class MainWindow : Window
{
public class Student:DependencyObject
{
public static readonly DependencyProperty NameProperty =
DependencyProperty.Register("Name", typeof(string), typeof(Student));
}
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Student stu = new Student();
stu.SetValue(Student.NameProperty, this.textBox1.Text);
textBox2.Text = (string)stu.GetValue(Student.NameProperty);
}
}
查看代码:
public class Student:DependencyObject
{
public static readonly DependencyProperty NameProperty =
DependencyProperty.Register("Name", typeof(string), typeof(Student));
}
自定义类Student继承自DependencyObject,DependencyProperty实例的声明由public、static、readonly三个修饰符修饰,实例并非使用new操作符得到,而是使用DependencyProperty.Register方法生成。
查看DependencyProperty.Register方法,它的介绍如下:
//
// 摘要:
// Registers a dependency property with the specified property name, property type,
// and owner type.
//
// 参数:
// name:
// The name of the dependency property to register. The name must be unique within
// the registration namespace of the owner type.
//
// propertyType:
// The type of the property.
//
// ownerType:
// The owner type that is registering the dependency property.
//
// 返回结果:
// A dependency property identifier that should be used to set the value of a public
// static readonly field in your class. That identifier is then used to reference
// the dependency property later, for operations such as setting its value programmatically
// or obtaining metadata.
第一个参数为string类型,用这个参数来指明以哪个CLR属性作为这个依赖属性的包装器,或者说此依赖属性支持(back)的是哪个CLR属性。
第二个参数用来指明此依赖属性用来存储什么类型的值,学生的姓名是string类型,所以这个参数被赋值为typeof(string)。
第三个参数用来指明此依赖属性的宿主是什么类型。本例中想为Student类准备一个可依赖的名称属性,所以需要把NameProperty注册成与Student关联,因此这个参数被赋值为typeof(Student)。
Button的点击事件代码如下:
private void Button_Click(object sender, RoutedEventArgs e)
{
Student stu = new Student();
stu.SetValue(Student.NameProperty, this.textBox1.Text);
textBox2.Text = (string)stu.GetValue(Student.NameProperty);
}
第一句创建了一个Student类型的对象;第二句调用SetValue方法把textBox1.Text属性的值存储进依赖属性;第三句是使用GetValue方法把值读取出来,因为GetValue的返回值是Object类型,所以要进行适当的类型转换,将其显示在textBox2上。
运行的效果如下所示:

参考书籍
《深入浅出WPF》——刘铁锰
原文始发于微信公众号(DotNet学习交流):WPF中的依赖属性
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/230850.html