依赖属性之附加属性
為何使用附加屬性(摘自MSDN)?????
附加屬性的一個用途是允許不同的子元素為實際在父元素中定義的屬性指定唯一值。? 此方案的一個具體應(yīng)用是讓子元素通知父元素它們將如何在用戶界面 (UI) 中呈現(xiàn)。? 一個示例是 DockPanel.Dock 屬性。? DockPanel.Dock?? 屬性創(chuàng)建為一個附加屬性,因為它設(shè)計為在 DockPanel 中包含的元素上設(shè)置,而不是在 DockPanel 本身上設(shè)置。? DockPanel?? 類定義名為 DockProperty 的靜態(tài) DependencyProperty 字段,然后將 GetDock 和 SetDock 方法作為該附加屬性的公共訪問器提供。
?
如何創(chuàng)建附加屬性?(摘自MSDN)????
如果您的類將附加屬性嚴格定義為用于其他類型,那么該類不必從 DependencyObject 派生。?但是,如果您遵循總體 WPF 模型具有該附加屬性也是依賴項屬性,但是,需要從 DependencyObject 派生。?
通過聲明一個 DependencyProperty 類型的 public?static?readonly 字段可以將附加屬性定義為依賴項屬性。通過使用 RegisterAttached 方法的返回值來定義此字段。? 為了遵循命名標識字段及其所表示的屬性的已建立 WPF 模式,字段名必須與附加屬性名一致,并附加字符串 Property。? 附加屬性提供程序還必須提供靜態(tài) GetPropertyName 和 SetPropertyName 方法作為訪問器為附加屬性;否則會導致屬性系統(tǒng)無法使用附加屬性。
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/
轉(zhuǎn)載于:https://www.cnblogs.com/demo8/p/3266254.html
總結(jié)
- 上一篇: Deep Belief Network简
- 下一篇: Oracle10g 64位 在Windo