WPF学习笔记——依赖属性(Dependency Property)

1、什么是依赖属性

依赖属性是一种可以自己没有值,并且通过Binding从数据源获得值(依赖在别人身上)的属性,拥有依赖属性的对象被称为“依赖对象”。

依赖项属性通过调用 Register 方法(或 RegisterReadOnly)在 WPF 属性系统中注册,并通过 DependencyProperty 标识符标示属性。 依赖项属性只能由继承自 DependencyObject 类的类型使用,在WPF中大部分的类都可以支持依赖属性。

2、DependencyObject和DependencyPorperty

DependencyObject和DependencyPorperty两个类是WPF属性系统的核心

在WPF中,依赖对象的概念被DependencyObject类实现;依赖属性的概念则由DependencyProperty类实现

必须使用依赖对象作为依赖属性的宿主,二者结合起来,才能实现完整的Binding目标被数据所驱动。DependencyObject具有GetValue和SetValue两个方法,用来获取/设置依赖属性的值。

DependencyProperty实例的声明特点——引用变量由public static readonly三个修饰符修饰,实例不是new 操作符得到,而是调用DependencyProperty.Register方法生成。(见实例代码)

3、示例代码

<Window x:Class="DependencyPropertyDemo.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:DependencyPropertyDemo"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <TextBox x:Name="textBox1" BorderBrush="Black" Margin="5"/>
        <TextBox x:Name="textBox2" BorderBrush="Black" Margin="5" Text="{Binding Path=Text, ElementName=textBox1,Mode=OneWay}"/>
        <Button Margin="5" Content="OK" Click="Button_Click"/>
    </StackPanel>
</Window>

声明一个Student类,继承DependencyObject,在里面声明一个属性,即依赖属性,命名规则是要以Property结尾。

为了方便使用,给他声明了一个Name属性,set中调用SetValue()方法设置Name,get中调用GetValue()方法获取Name。

  public class Student:DependencyObject
    {
    

/// <summary>
/// Register方法中各参数说明
/// 第一个参数为string类型,指明以那个CLR属性作为依赖属性的包装器,这里是Name,已经实现CLR属性的包装
/// 第二各参数为Type类型,用来指明次依赖属性用来存储什么类型的值
/// 第三个参数为Type类型,指明依赖属性宿主类型,或者说DependencyProperty.Register放啊分将把这个依赖属性关联到哪个类型上。
/// </summary>

public static readonly DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof(string), typeof(Student));

        public string Name
        {
            get { return (string)this.GetValue(Student.NameProperty); }
            set { this.SetValue(Student.NameProperty, value); }
        }
    }
 Student stu;

        public MainWindow()
        {
            InitializeComponent();
            stu = new Student();
            Binding binding = new Binding("Text") { Source = textBox1 };
            BindingOperations.SetBinding(stu, Student.NameProperty, binding);
           // textBox2.SetBinding(TextBox.TextProperty, binding);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(stu.Name);

        }

在MainWindow()中的最后两行代码,绑定Student实例和textbox,textbox作为数据源,而Student实例作为目标,目标的属性为NameProperty。

如此便实现了前台TextBox输入字符串和后台的Student实例的数据绑定。

转载于:https://www.cnblogs.com/maodexie/p/5936995.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值