日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

依赖属性之附加属性

發布時間:2025/6/17 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 依赖属性之附加属性 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

為何使用附加屬性(摘自MSDN)?????

附加屬性的一個用途是允許不同的子元素為實際在父元素中定義的屬性指定唯一值。? 此方案的一個具體應用是讓子元素通知父元素它們將如何在用戶界面 (UI) 中呈現。? 一個示例是 DockPanel.Dock 屬性。? DockPanel.Dock?? 屬性創建為一個附加屬性,因為它設計為在 DockPanel 中包含的元素上設置,而不是在 DockPanel 本身上設置。? DockPanel?? 類定義名為 DockProperty 的靜態 DependencyProperty 字段,然后將 GetDock 和 SetDock 方法作為該附加屬性的公共訪問器提供。

?

如何創建附加屬性?(摘自MSDN)????

如果您的類將附加屬性嚴格定義為用于其他類型,那么該類不必從 DependencyObject 派生。?但是,如果您遵循總體 WPF 模型具有該附加屬性也是依賴項屬性,但是,需要從 DependencyObject 派生。?

通過聲明一個 DependencyProperty 類型的 public?static?readonly 字段可以將附加屬性定義為依賴項屬性。通過使用 RegisterAttached 方法的返回值來定義此字段。? 為了遵循命名標識字段及其所表示的屬性的已建立 WPF 模式,字段名必須與附加屬性名一致,并附加字符串 Property? 附加屬性提供程序還必須提供靜態 GetPropertyName 和 SetPropertyName 方法作為訪問器為附加屬性;否則會導致屬性系統無法使用附加屬性。

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls;namespace PropertyDemo {public static class PasswordBoxHelper{public static readonly DependencyProperty PasswordProperty =DependencyProperty.RegisterAttached("Password",typeof(string), typeof(PasswordBoxHelper),new FrameworkPropertyMetadata(string.Empty, OnPasswordPropertyChanged));public static readonly DependencyProperty AttachProperty =DependencyProperty.RegisterAttached("Attach",typeof(bool), typeof(PasswordBoxHelper), new PropertyMetadata(false, Attach));private static readonly DependencyProperty IsUpdatingProperty =DependencyProperty.RegisterAttached("IsUpdating", typeof(bool),typeof(PasswordBoxHelper));public static void SetAttach(DependencyObject dp, bool value){dp.SetValue(AttachProperty, value);}public static bool GetAttach(DependencyObject dp){return (bool)dp.GetValue(AttachProperty);}public static string GetPassword(DependencyObject dp){return (string)dp.GetValue(PasswordProperty);}public static void SetPassword(DependencyObject dp, string value){dp.SetValue(PasswordProperty, value);}private static bool GetIsUpdating(DependencyObject dp){return (bool)dp.GetValue(IsUpdatingProperty);}private static void SetIsUpdating(DependencyObject dp, bool value){dp.SetValue(IsUpdatingProperty, value);}private static void OnPasswordPropertyChanged(DependencyObject sender,DependencyPropertyChangedEventArgs e){PasswordBox passwordBox = sender as PasswordBox;passwordBox.PasswordChanged -= PasswordChanged;if (!(bool)GetIsUpdating(passwordBox)){passwordBox.Password = (string)e.NewValue;}passwordBox.PasswordChanged += PasswordChanged;}private static void Attach(DependencyObject sender,DependencyPropertyChangedEventArgs e){PasswordBox passwordBox = sender as PasswordBox;if (passwordBox == null)return;if ((bool)e.OldValue){passwordBox.PasswordChanged -= PasswordChanged;}if ((bool)e.NewValue){passwordBox.PasswordChanged += PasswordChanged;}}private static void PasswordChanged(object sender, RoutedEventArgs e){PasswordBox passwordBox = sender as PasswordBox;SetIsUpdating(passwordBox, true);SetPassword(passwordBox, passwordBox.Password);SetIsUpdating(passwordBox, false);}} }

以上例子來源:http://home.cnblogs.com/u/li-peng/

轉載于:https://www.cnblogs.com/demo8/p/3266254.html

總結

以上是生活随笔為你收集整理的依赖属性之附加属性的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。