Windows 8 Store Apps Learning (16) Control Basics: Dependency properties, and so on

Source: Internet
Author: User
Tags inheritance xmlns

Control Basics: Dependency properties, attached properties, control inheritance relationships, routed events, and hit tests

Introduced

Re-imagine the control basics for Windows 8 Store Apps

DependencyProperty-Dependency Properties

Attachedproperty-Attached properties

Inheritance relationships for controls

Routed events and hit tests

Example

1. Develop a custom control with DependencyProperty and 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 ">
    
    <!--
        The style of the custom control is defined in this document (THEMES/GENERIC.XAML) to
        
        integrate the external resourcedictionary
    -->
    < resourcedictionary.mergeddictionaries>
        <resourcedictionary source= "ms-appx:///mycontrols/themes/ Mycontrol.xaml "/>
    </ResourceDictionary.MergedDictionaries>
       
</ResourceDictionary>

Mycontrols/themes/mycontrol.xaml

<resourcediction ary xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "Http://schema"
        S.microsoft.com/winfx/2006/xaml "xmlns:local=" Using:mycontrols "> <style targettype=" Local:MyControl "> <setter property= "Template" > <Setter.Value> <controltemplate targettype= "L  Ocal:mycontrol "> <border background=" {templatebinding Background} "borderbrush=" {TemplateBinding
                            BorderBrush} "borderthickness=" {templatebinding borderthickness} "> <StackPanel> <!--binding Custom dependency property--> <textblock text= "{templatebinding Title}" Fore
                            ground= "White" fontsize= "26.667"/> <!--binding Custom attached properties-->
                        <textblock text= "{templatebinding Local:MyAttachedProperty.SubTitle}" foreground= "White" fontsize= "14.667"/> </stackpanel> </Border> </ControlTemplate> </setter.value&
        Gt </Setter> </Style> </ResourceDictionary>

Mycontrols/mycontrol.cs

* * Develop a custom control to demonstrate dependency properties (Dependency property) and attached properties (attached property) * Dependency properties: Can be used for styles, templates, bindings, animations * Additional properties: Globally available dependency properties *
/using Windows.UI.Xaml.Controls;
    
Using Windows.UI.Xaml; namespace MyControls {///<summary>///for dependency properties Demo///</summary> public class Mycontrol:con Trol {Public mycontrol () {///specifies that the default style is typeof (MyControl), which corresponds to: <style xmlns:local= "using: MyControls "targettype=" Local:mycontrol "/> This.
        Defaultstylekey = typeof (MyControl);
        ///Access dependency properties through Dependencyobject.getvalue () and Dependencyobject.setvalue (), encapsulated by the Title attribute to facilitate access to dependent properties
            public string Title {get {return (string) GetValue (Titleproperty);}
        set {SetValue (Titleproperty, value);} }//Register a dependent property public static readonly DependencyProperty Titleproperty = Dependencypropert
               Y.register (Name of "Title",//Dependent property) typeof (String),//Dependent property data type typeof (MyControl),///dependency property belongs to class new PropertyMetadata ("", Pr Opertymetadatacallback)); Specifies the default value of the dependent property and the method that is invoked when the value is changed private static void Propertymetadatacallback (DependencyObject sender, Dependency PropertyChangedEventArgs args) {object newvalue = args. NewValue; The value object OldValue = args after the change occurs. OldValue; Value before changes}}///<summary>///for additional properties demo///</summary> public C
            Lass Myattachedproperty {//Get attached property public static string Getsubtitle (DependencyObject obj) { return (string) obj.
        GetValue (Subtitleproperty);
            //Set additional properties public static void Setsubtitle (DependencyObject obj, string value) { Obj.
        SetValue (subtitleproperty, value);
           }//Register an attached property public static readonly DependencyProperty Subtitleproperty = dependencyproperty.registerattached ("subtitle",//attached property name typeof (String),//attached property data type typeof (Myattachedproperty),//attached property to the class new PropertyMetadata ("", Propertymetadatacallback) ); Specifies the default value for the attached property, and the method that is invoked when the value is changed private static void Propertymetadatacallback (DependencyObject sender, Dependency PropertyChangedEventArgs args) {object newvalue = args. NewValue; The value object OldValue = args after the change occurs. OldValue; Value prior to change}}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.