WPF 依赖属性

依赖属性,它是注册到 Hastable 里面去了。依赖属性默认属性变更通知功能。

▲ 点击按钮 Traget 显示

▲ 点击按钮 Traget 显示

XAML:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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));
}
}

增加绑定功能:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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));
}
}

▲ 运行效果

▲ 运行效果

再包装一下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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

感谢支持!