附加属性本质也是依赖属性,依赖属性的宿主位置不一样。
XAML:
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d" WindowStartupLocation="CenterScreen"
Title="MainWindow" Height="250" Width="350">
<Grid>
<Button Content="Grade" Width="100" Height="50" Click="Button_Click"/>
</Grid>
</Window>
C# Behind:
namespace WpfApp1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Human human = new Human();
School.SetGrade(human, 6);
int grade = School.GetGrade(human);
MessageBox.Show(grade.ToString());
Student stu = new Student();
School.SetGrade(stu, 6);
int g = School.GetGrade(stu);
MessageBox.Show(g.ToString());
}
}
public class School:DependencyObject
{
public static int GetGrade(DependencyObject obj)
{
return (int)obj.GetValue(GradeProperty);
}
public static void SetGrade(DependencyObject obj, int value)
{
obj.SetValue(GradeProperty, value);
}
// Using a DependencyProperty as the backing store for Grade. This enables animation, styling, binding, etc...
public static readonly DependencyProperty GradeProperty =
DependencyProperty.RegisterAttached("Grade", typeof(int), typeof(School), new PropertyMetadata(0));
}
public class Human:DependencyObject
{
}
public class Student:DependencyObject
{
}
}
运行结果:
▲ 运行结果, 两个弹窗都显示 6 没有问题
以上可见,附加属性和依赖属性保存值过程是一样的。只是用来检索值的依赖属性(附加属性)并不是以Human
为宿主,而是寄宿在School
里面,而这是没有什么关系的。因为,CLR 属性名和宿主类型只是用来生成 hash code 和 GlobalIndex 的,保证键值对唯一。
另外,既然附加属性和依赖属性本质是一样的,那么附加属性也是可以用来 Binding 的。
XAML:
<Window x:Class="WpfApp1.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:WpfApp1"
mc:Ignorable="d" WindowStartupLocation="CenterScreen"
Title="MainWindow" Height="250" Width="350">
<Canvas>
<Slider x:Name="sliderX" Canvas.Top="10" Canvas.Left="10" Width="320" Minimum="50" Maximum="260"/>
<Slider x:Name="sliderY" Canvas.Top="50" Canvas.Left="10" Width="320" Minimum="50" Maximum="260"/>
<Rectangle x:Name="rect" Fill="Blue" Width="30" Height="30" Canvas.Left="{Binding ElementName=sliderX, Path=Value}"
Canvas.Top="{Binding Path=Value, ElementName=sliderY}" />
</Canvas>
</Window>
▲ 移动两个滑条,方块会移动位置
参考: 《WPF 深入浅出》- P150