Windows Phone (13) style inheritance

Source: Internet
Author: User

In the previous article, we have introduced the ability to define styles in the resources set. We can also reference other styles in one style. This is the concept of inheritance, the reference style is placed in the baseon attribute of the style. Here, the XAML markup extension is used for setting. For example, the three styles defined here:

 <phone:PhoneApplicationPage.Resources>        <Style x:Key="tbStyle" TargetType="TextBlock">            <Setter Property="HorizontalAlignment" Value="Center"></Setter>            <Setter Property="HorizontalAlignment" Value="Center"></Setter>            <Setter Property="Foreground">                <Setter.Value>                    <LinearGradientBrush>                        <GradientStop Offset="0.2" Color="Brown"></GradientStop>                        <GradientStop Offset="0.7" Color="DarkBlue"></GradientStop>                    </LinearGradientBrush>                </Setter.Value>            </Setter>        </Style>        <Style x:Key="fStyle" TargetType="TextBlock">            <Setter Property="VerticalAlignment" Value="Bottom"></Setter>        </Style>        <Style x:Key="tStyle" TargetType="TextBlock" BasedOn="{StaticResource tbStyle}">            <Setter Property="VerticalAlignment" Value="Top"></Setter>        </Style>    </phone:PhoneApplicationPage.Resources>

From the code above, we can see that the third style inherits the first style, then the vertical position defined in the first style, and the third also defines the vertical position, then we use the third style from textblock.

<Textblock X: Name = "tbcontent" text = "display style" style = "{staticresource tstyle}"/>

The effect is as follows:

 

This indicates that the attributes in the third style overwrite the same attributes in the first style. Note that the preceding three styles are sequential, that is, the following can inherit the preceding attributes, however, the system will prompt a warning that the style you want to inherit cannot be found. What will happen if the styles in these three styles are cascade inherited: the Code is as follows:

 

<phone:PhoneApplicationPage.Resources>        <Style x:Key="tbStyle" TargetType="TextBlock">            <Setter Property="HorizontalAlignment" Value="Center"></Setter>            <Setter Property="HorizontalAlignment" Value="Center"></Setter>            <Setter Property="Foreground">                <Setter.Value>                    <LinearGradientBrush>                        <GradientStop Offset="0.2" Color="Brown"></GradientStop>                        <GradientStop Offset="0.7" Color="DarkBlue"></GradientStop>                    </LinearGradientBrush>                </Setter.Value>            </Setter>        </Style>        <Style x:Key="tStyle" TargetType="TextBlock" BasedOn="{StaticResource tbStyle}">            <Setter Property="VerticalAlignment" Value="Top"></Setter>            <Setter Property="HorizontalAlignment" Value="Left"></Setter>        </Style>        <Style x:Key="fStyle" TargetType="TextBlock" BasedOn="{StaticResource tStyle}">            <Setter Property="VerticalAlignment" Value="Bottom"></Setter>        </Style>           </phone:PhoneApplicationPage.Resources>

Then textblock uses the third Style

<Textblock X: Name = "tbcontent" text = "display style" style = "{staticresource fstyle}"/>

The effect is like this.

 

So we can sum up and define three or more styles, such as A, B, and C. If B inherits a and c inherits B, the priority is C> B>, it can also be said that the inheritance of style is higher and the priority is lower;

I will share with you the following link:

 

WP7 development and learning (4): Four styles ---- Summary of styles in WP

 

Dependency properties in WPF-dependency properties are finally understood

 

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.