wpf 自定义依赖性属性 作用之一 对数据绑定的支持
2014-04-04 15:50
246 查看
依赖属性:定义,声明,注册
依赖属性,在数据绑定中,数据绑定,分为源对象(数据源)和目标对象(显示数据)。
只有源对象为依赖对象,属性为依赖属性时,该属性才会在属性发生变化时,通知目标对象进行数据更改。
依赖属性,具有对目标对象更改通知的功能。
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));
}
}
依赖属性,在数据绑定中,数据绑定,分为源对象(数据源)和目标对象(显示数据)。
只有源对象为依赖对象,属性为依赖属性时,该属性才会在属性发生变化时,通知目标对象进行数据更改。
依赖属性,具有对目标对象更改通知的功能。
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));
}
}
相关文章推荐
- 让ListBox的SelectedItems属性支持数据绑定
- WPF的xaml中导入其他命名空间以及绑定类属性数据
- 稳扎稳打Silverlight(64) - 5.0绑定之 Style 中的 Setter 支持绑定, 绑定父级链上的元素, 隐式指定数据模板, UI 上数据更新的触发方式
- 打造Android集合控件数据绑定(支持添加监听,支持AbsListView与RecycleView,支持异步加载等)(一)基础篇
- C#:LinkButton 控件 绑定数据时候 相关属性的注解
- VUE的进阶 标签属性数据绑定和拼接
- AngularJS 作用域与数据绑定机制
- iOS Json和对象互转(反射支持对象嵌套的复杂对象类型,支持基本数据类型和NSDate属性转化)
- Xamarin XAML语言教程使用Progress属性数据绑定设置进度条进度
- 设置disabled属性后台读不到数据如何让select支持readonly
- SAPUI5 (11) - 数据绑定之属性绑定(property binding)
- WPF 依赖属性,用户控件依赖属性(DependencyProperty 依赖属性、GetValue() SetValue() CLR属性包装器、SetBinding 数据绑定)
- Effective C# 定制和支持数据绑定
- vue div contenteditable属性,模拟v-model双向数据绑定功能
- [Silverlight]实现到自定义类型的属性数据绑定
- Vue.js总结学习(指令、计算属性、数据绑定)
- ssm后端校验,解决数据无法回显和jsp页面属性无法绑定userBean
- NetBox环境下ajax用get获取数据,中文乱码的问题解决:对象不支持此属性或方法: 'Response.CharSet'
- AspNetAjaxPager,Asp.Net通用无刷新Ajax分页控件,支持多样式多数据绑定
- js导出数据,浏览器(内核必须支持<a> download 属性或Blob对象创建)兼容