WPF 依赖属性
依赖属性,它是注册到 Hastable 里面去了。依赖属性默认属性变更通知功能。
▲ 点击按钮 Traget 显示
XAML:
<Window x:Class="WPF_P128.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:WPF_P128"
mc:Ignorable="d"
Title="DependencyProperty" Height="160" Width="260">
<Grid>
<StackPanel>
<TextBlock Text="Source:" Margin="5 5 5 0"/>
<TextBox x:Name="textBox1" BorderBrush="Black" Margin="5 0 5 0"/>
<TextBlock Text="Target:" Margin="5 5 5 0"/>
<TextBox x:Name="textBox2" BorderBrush="Black" Margin="5 0 5 0"/>
<Button Content="OK" Margin="5" Click="Button_Click"/>
</StackPanel>
</Grid>
</Window>
DependencyObject
类内有两个方法:object GetValue(DependencyProperty dp)
和 Void SetValue(DependencyProperty dp, object o)
C# Behind:
namespace WPF_P128
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Student stu = new Student();
stu.SetValue(Student.NameProperty, 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));
}
}
增加绑定功能:
namespace WPF_P128
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// 增加绑定方式
stu = new Student();
Binding binding = new Binding("Text") { Source = textBox1 };
BindingOperations.SetBinding(stu, Student.NameProperty, binding);
}
private Student stu = null;
private void Button_Click(object sender, RoutedEventArgs e)
{
// 直接赋值,取值
//Student stu = new Student();
stu.SetValue(Student.NameProperty, textBox1.Text);
textBox2.Text = (string)stu.GetValue(Student.NameProperty);
// 弹窗显示绑定属性值
MessageBox.Show(stu.GetValue(Student.NameProperty).ToString());
}
}
public class Student:DependencyObject
{
// 依赖属性
public static readonly DependencyProperty NameProperty =
DependencyProperty.Register("Name", typeof(string), typeof(Student));
}
}
▲ 运行效果
再包装一下:
namespace WPF_P128
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// 增加绑定方式
stu = new Student();
Binding binding = new Binding("Text") { Source = textBox1, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged };
//BindingOperations.SetBinding(stu, Student.NameProperty, binding);
stu.SetBinding(Student.NameProperty, binding);
textBox2.SetBinding(TextBox.TextProperty, new Binding(nameof(stu.Name)) { Source = stu });
}
private Student stu = null;
private void Button_Click(object sender, RoutedEventArgs e)
{
// 直接赋值,取值
//Student stu = new Student();
//stu.SetValue(Student.NameProperty, textBox1.Text);
//textBox2.Text = (string)stu.GetValue(Student.NameProperty);
// 弹窗显示绑定属性值
MessageBox.Show(stu.GetValue(Student.NameProperty).ToString());
}
}
public class Student:DependencyObject
{
// 依赖属性
public static readonly DependencyProperty NameProperty =
DependencyProperty.Register("Name", typeof(string), typeof(Student));
// CLR 属性包装
public string Name
{
get { return (string)GetValue(NameProperty); }
set { SetValue(NameProperty, value); }
}
// SetBinding 包装
public BindingExpressionBase SetBinding(DependencyProperty dp, BindingBase binding)
{
return BindingOperations.SetBinding(this, dp, binding);
}
}
}
运行效果:
▲ 运行效果
参考: 《WPF 深入浅出》 - P138
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了