WPF 附加属性

附加属性本质也是依赖属性,依赖属性的宿主位置不一样。

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

posted @   double64  阅读(205)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示