#眉標=Vista開發技術 #副標=Windows Presentation Foundation(3) #大標=Dependency Property #作者=文/蔡學鏞 ============= Window Grid Button1 TextBlock1 Button2 TextBlock2 ================ ============= Object DispatcherObject (abstract) DependencyObject Visual (abstract) UIElement FrameworkElement Control ================ ==程式1 =========== public double FontSize { get { return fntsize; } set { fntsize = value; ... } } ================ ==程式2 =========== public class Control: FrameworkElement { ... public static readonly DependencyProperty FontSizeProperty; ... } ================ ==程式3 =========== FontSizeProperty = DependencyProperty.Register("FontSize", typeof (double), typeof(Control)); ================ ==程式3 =========== public class Control: FrameworkElement { ... public static readonly DependencyProperty FontSizeProperty; ... static Control() { FrameworkPropertyMetadata metadata = new FrameworkPropertyMetadata(); metadata.DefaultValue = 11; metadata.AffectsMeasure = true; metadata.Inherits = true; metadata.IsDataBindingAllowed = true; metadata.DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged; FontSizeProperty = DependencyProperty.Register("FontSize", typeof(double), typeof(Control), metadata, ValidateFontSize); } static bool ValidateFontSize(object obj) { double dFontSize = (double) obj; return dFontSize > 0 && dFontSize <= 35791; } ... } ================ ==程式4 =========== public class Control: FrameworkElement { ... public double FontSize { set { SetValue(FontSizeProperty, value); } get { return (double) GetValue(FontSizeProperty); } } ... } ================ ==關於作者=========== 蔡學鏞 清華大學資訊工程碩士。曾任軟體公司經理、大學講師。著有「Java夜未眠」,譯有「深入淺出設計模式」等書,專欄包括「台灣微軟MSDN.NET大內高手」。目前的研究領域在Natural Language Processing。 ================