winfwu

导航

WPF Style设置的二种方法

效果如下:

 

 

案例一描述:

TargetType 设置所有TextBlock样式

    <s:SurfaceWindow.Resources>

    <Style TargetType="{x:Type TextBlock}">

        <Setter Property="FontFamily" Value="Segoe Black" />

        <Setter Property="HorizontalAlignment" Value="Center" />

        <Setter Property="FontSize" Value="12pt" />

        <Setter Property="Foreground" Value="#777777" />

            <Setter Property="Background" Value="Yellow"/>

        </Style>

    </s:SurfaceWindow.Resources>

   

    <Grid>

        <TextBlock >你好!</TextBlock>

    </Grid>

 

案例二描述:

通过key-StaticResource方式获取style中设置的参数:

注意:不同于TargetType的是key-StaticResource 必须用“Control.FontFamily”的方式来设置。

    <s:SurfaceWindow.Resources>

        <Style x:Key="style1">

            <Setter Property="Control.FontFamily" Value="Segoe Black" />

            <Setter Property="Control.HorizontalAlignment" Value="Center" />

            <Setter Property="Control.FontSize" Value="12pt" />

            <Setter Property="Control.Foreground" Value="#777777" />

            <Setter Property="Control.Background" Value="Yellow"/>

        </Style>

    </s:SurfaceWindow.Resources>

    <Grid>

        <Label Content="你好!" Style="{StaticResource style1}"></Label>

    </Grid>

 

posted on 2013-02-18 16:40  winfwu  阅读(2340)  评论(0编辑  收藏  举报