控制項基礎: 相依性屬性, 附加屬性, 控制項的繼承關係, 路由事件和點擊測試
介紹
重新想象 Windows 8 Store Apps 之 控制項基礎
DependencyProperty - 相依性屬性
AttachedProperty - 附加屬性
控制項的繼承關係
路由事件和點擊測試
樣本
1、開發一個具有 DependencyProperty 和 AttachedProperty 的自訂控制項
MyControls/themes/generic.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:MyControls"> <!-- 自訂控制項的樣式在本檔案(themes/generic.xaml)中定義 整合外部 ResourceDictionary --> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="ms-appx:///MyControls/themes/MyControl.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary>
MyControls/themes/MyControl.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:MyControls"> <Style TargetType="local:MyControl"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="local:MyControl"> <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <StackPanel> <!--綁定自訂相依性屬性--> <TextBlock Text="{TemplateBinding Title}" Foreground="White" FontSize="26.667" /> <!--綁定自訂附加屬性--> <TextBlock Text="{TemplateBinding local:MyAttachedProperty.SubTitle}" Foreground="White" FontSize="14.667" /> </StackPanel> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style></ResourceDictionary>
MyControls/MyControl.cs
/* * 開發一個自訂控制項,用於示範相依性屬性(Dependency Property)和附加屬性(Attached Property) * * 相依性屬性:可以用於樣式, 模板, 綁定, 動畫 * 附加屬性:全域可用的相依性屬性 */ using Windows.UI.Xaml.Controls;using Windows.UI.Xaml; namespace MyControls{ /// <summary> /// 用於相依性屬性的示範 /// </summary> public class MyControl : Control { public MyControl() { // 指定預設樣式為 typeof(MyControl),即對應:<Style xmlns:local="using:MyControls" TargetType="local:MyControl" /> this.DefaultStyleKey = typeof(MyControl); } // 通過 DependencyObject.GetValue() 和 DependencyObject.SetValue() 訪問相依性屬性,這裡由 Title 屬性封裝一下,以方便對相依性屬性的訪問 public string Title { get { return (string)GetValue(TitleProperty); } set { SetValue(TitleProperty, value); } } // 註冊一個相依性屬性 public static readonly DependencyProperty TitleProperty = DependencyProperty.Register( "Title", // 相依性屬性的名稱 typeof(string), // 相依性屬性的資料類型 typeof(MyControl), // 相依性屬性所屬的類 new PropertyMetadata("", PropertyMetadataCallback)); // 指定相依性屬性的預設值,以及值發生改變時所調用的方法 private static void PropertyMetadataCallback(DependencyObject sender, DependencyPropertyChangedEventArgs args) { object newValue = args.NewValue; // 發生改變之後的值 object oldValue = args.OldValue; // 發生改變之前的值 } } /// <summary> /// 用於附加屬性的示範 /// </summary> public class MyAttachedProperty { // 擷取附加屬性 public static string GetSubTitle(DependencyObject obj) { return (string)obj.GetValue(SubTitleProperty); } // 設定附加屬性 public static void SetSubTitle(DependencyObject obj, string value) { obj.SetValue(SubTitleProperty, value); } // 註冊一個附加屬性 public static readonly DependencyProperty SubTitleProperty = DependencyProperty.RegisterAttached( "SubTitle", // 附加屬性的名稱 typeof(string), // 附加屬性的資料類型 typeof(MyAttachedProperty), // 附加屬性所屬的類 new PropertyMetadata("", PropertyMetadataCallback)); // 指定附加屬性的預設值,以及值發生改變時所調用的方法 private static void PropertyMetadataCallback(DependencyObject sender, DependencyPropertyChangedEventArgs args) { object newValue = args.NewValue; // 發生改變之後的值 object oldValue = args.OldValue; // 發生改變之前的值 } }}