wpf 自定义依赖性属性 作用之一 对数据绑定的支持

 

依赖属性:定义,声明,注册 

 

依赖属性,在数据绑定中,数据绑定,分为源对象(数据源)和目标对象(显示数据)。

只有源对象为依赖对象,属性为依赖属性时,该属性才会在属性发生变化时,通知目标对象进行数据更改。

依赖属性,具有对目标对象更改通知的功能。

 

XAML

<StackPanel>
<TextBox Style="{StaticResource textStyle}" Height="37" Name="textBox1" FontSize="26" Margin="5" Width="439" />
<TextBox Style="{StaticResource textStyle}" Height="37" Name="textBox2" FontSize="26" Margin="5" Width="439" />
<Button Content="Button" Height="39" Name="button1" Width="131" Click="button1_Click" />
</StackPanel>

 

.CS

namespace WPF_VIP_Characters
{
/// <summary>
/// Interaction logic for DependProperty.xaml
/// </summary>
public partial class DependProperty : Window
{
public DependProperty()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
//Student stu = new Student();
//stu.SetValue(Student.NameProperty, textBox1.Text);
//textBox2.Text = (string)stu.GetValue(Student.NameProperty);

Student stu = new Student();

Binding binding = new Binding("Text") { Source = textBox1 };
BindingOperations.SetBinding(stu, Student.NameProperty, binding);

Binding binding2 = new Binding("Name") { Source = stu };
BindingOperations.SetBinding(textBox2, TextBox.TextProperty, binding2);

}
}

class Student:DependencyObject
{
//CLR属性进行封装
public string Name
{
get { return (string)GetValue(NameProperty); }
set { SetValue(NameProperty, value); }
}
//定义依赖属性/注册
public static readonly DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof(string), typeof(Student));
}

}

 

转载于:https://www.cnblogs.com/wwwfj/p/3645319.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值