建筑模拟3中文版下载,厦门网站seo外包,教育网站 网页赏析,广告设计制作合同模板非常重要
依赖属性和附加属性#xff0c;两者是有关系的#xff0c;也是有些区别的#xff0c;很多时候#xff0c;可能会把两者混淆了。
附加属性#xff08;Attach Property#xff09; 顾名思义#xff0c;就是附加上面的属性#xff0c;自身是没有的#xff0c;… 非常重要
依赖属性和附加属性两者是有关系的也是有些区别的很多时候可能会把两者混淆了。
附加属性Attach Property 顾名思义就是附加上面的属性自身是没有的别人附加上面的就变成了自己的属性就可以使用点. 点击 出来。比如说wpf中PasswordBox控件是不能进行绑定数据的但是你把它绑定一个密码那么就是附加属性了。附加属性也属于一种依赖属性。
1.附加属性建立输入propa点击tab按钮2次 2.建立Password类修改对应的参数
可见独立创建一个类附加到PasswordBox控件上面的属性。
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
Window x:ClassWpfApp5.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:WpfApp5mc:IgnorabledTitleMainWindow Height450 Width800Grid PasswordBox local:Password.Password{Binding PW} NameAA HorizontalAlignmentLeft Margin416,183,0,0 VerticalAlignmentTop Width120/PasswordBox HorizontalAlignmentLeft Password12313 Margin20 NameAA1 VerticalAlignmentTop Width120/Button Width200 Height50 Margin176,246,424,139 ClickButton_Click1/Button/Grid
/Window4.CS文件中写法
此时PasswordBox可以绑定PW的值如果没有附加属性的话那么原生的PasswordBox是不能直接赋值PW的也就是没有Binding的功能附加属性就是增加了Binding的功能。 源码
https://download.csdn.net/download/u012563853/88623271
来源
WPF-附加属性《十二》-CSDN博客