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

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