Windows 8 Store Apps學習(16) 控制項基礎: 相依性屬性等等

來源:互聯網
上載者:User

控制項基礎: 相依性屬性, 附加屬性, 控制項的繼承關係, 路由事件和點擊測試

介紹

重新想象 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; // 發生改變之前的值        }    }}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.