WPF-附加属性《十二》

非常重要

依赖属性和附加属性,两者是有关系的,也是有些区别的,很多时候,可能会把两者混淆了。

附加属性(Attach Property)

顾名思义,就是附加上面的属性,自身是没有的,别人附加上面的,就变成了自己的属性,就可以使用点. 点击 出来。比如说,wpf中PasswordBox控件是不能进行绑定数据的,但是你把它绑定一个密码,那么就是附加属性了。附加属性,也属于一种依赖属性。

1.附加属性建立,输入propa,点击tab按钮2次

2.建立Password类,修改对应的参数

可见,独立创建一个类,附加到PasswordBox控件上面的属性。

cs 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace WpfApp5
{
    public class Password
    {
        public static string GetPassword(DependencyObject obj)
        {
            return (string)obj.GetValue(MyPassword);
        }

        public static void SetPassword(DependencyObject obj, string value)
        {
            obj.SetValue(MyPassword, value);
        }

        // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MyPassword =
            DependencyProperty.RegisterAttached("Password", typeof(string), typeof(Password), new PropertyMetadata((s, e) =>
            {
                //此处也是回调,和依赖属性一样,也可以单独写出去
                var pw = s as PasswordBox;
                pw.Password = e.NewValue.ToString();   //这里和xaml中建立关系
            }
            ));
    }
}

3.前端xaml

cs 复制代码
<Window x:Class="WpfApp5.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:WpfApp5"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid> 
        <PasswordBox local:Password.Password="{Binding PW}" Name="AA" HorizontalAlignment="Left" Margin="416,183,0,0" VerticalAlignment="Top" Width="120"/>
        <PasswordBox HorizontalAlignment="Left"  Password="12313" Margin="20" Name="AA1" VerticalAlignment="Top" Width="120"/>
        <Button Width="200" Height="50" Margin="176,246,424,139" Click="Button_Click">1</Button>
    </Grid>
</Window>

4.CS文件中写法

此时,PasswordBox可以绑定PW的值,如果没有附加属性的话,那么原生的PasswordBox是不能直接赋值PW的,也就是没有Binding的功能,附加属性就是增加了Binding的功能。

源码

https://download.csdn.net/download/u012563853/88623271

来源:

WPF-附加属性《十二》-CSDN博客

相关推荐
cn_mengbei1 天前
鸿蒙PC原生应用开发实战:ArkTS与DevEco Studio从零构建跨端桌面应用全栈指南
华为·wpf·harmonyos
lingxiao168881 天前
WebApi详解+Unity注入--上篇:基于Framework的WebApi
c#·wpf·web
是一个Bug1 天前
Java后端开发面试题清单(50道) - 分布式基础
java·分布式·wpf
无心水1 天前
【分布式利器:腾讯TSF】4、TSF配置中心深度解析:微服务动态配置的终极解决方案
分布式·微服务·架构·wpf·分布式利器·腾讯tsf·分布式利器:腾讯tsf
lingxiao168882 天前
WebApi详解+Unity注入--下篇:Unity注入
unity·c#·wpf
无心水2 天前
【分布式利器:腾讯TSF】6、TSF可观测性体系建设实战:Java全链路Metrics+Tracing+Logging落地
java·分布式·架构·wpf·分布式利器·腾讯tsf·分布式利器:腾讯tsf
故事不长丨3 天前
C#字典(Dictionary)全面解析:从基础用法到实战优化
开发语言·c#·wpf·哈希算法·字典·dictionary·键值对
冰茶_3 天前
WPF路由事件:隧道与冒泡机制解析
学习·c#·.net·wpf·.netcore·mvvm
He BianGu3 天前
【笔记】 WPF中CollectionChangedEventManager功能详细介绍
笔记·wpf
张人玉3 天前
C#WPF页面布局及其属性
开发语言·c#·wpf