windo phone8.1 样式的基本使用(一)

                      样式的基本使用(一)

当一个项目中有多个控件出现相同的属性设置,那么可以使用以下解决办法

 

方法一:

<Page.Resources>

    <!--向资源字典中添加一个键为Buttonground,值为SolidColorBrush对象-->

    <SolidColorBrush 

            x:Key="ButtonBackground"

            Color="Aqua"/>

    <SolidColorBrush

            x:Key="ButtonForeground"

            Color="Red"/>

    <x:Double x:Key="ButtonFontSize">30</x:Double>

</Page.Resources>

<Grid>

    <Button

            Content="Button"

            Background="{StaticResource ButtonBackground}"

            Foreground="{StaticResource ButtonForeground}"

            FontSize="{StaticResource ButtonFontSize}"/>

</Grid>

 

方法二:

<Page.Resources>

    <Page.Resources>

        <!--类型选择器-->

        <!--键是唯一的,不能重复定义-->

        <!--style节点可以不用指定一个具体的键,有一个默认的键(typeof(Button))-->

        <Style TargetType="Button">

            <Setter Property="Width" Value="200"/>

        </Style>

        <!--解决类型选择器重名的问题-->

        <Style x:Key="ButtontowStyle" TargetType="Button">

            <Setter Property="Width" Value="300"/>

            <Setter Property="Background">

                <Setter.Value>

                    <SolidColorBrush Color="Aqua"/>

                </Setter.Value>

            </Setter>

        </Style>

    </Page.Resources>

    <StackPanel>

        <Button

            Content="Button1"/>

        <!--Button的Style默认指向的键为this.GetType()-->

        <Button

            Content="Button2"

            Style="{StaticResource ButtontowStyle}"/>

</StackPanel>

 

 

 

 

原文地址:https://www.cnblogs.com/fenger-VIP/p/4268937.html