鸟语天空
WPF—Style实现继承
post by:追风剑情 2019-8-27 16:14

示例

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfTest"
    xmlns:Properties="clr-namespace:WpfTest.Properties"
    xmlns:wpf="http://schemas.microsoft.com/netfx/2007/xaml/presentation" x:Class="WpfTest.MainWindow"
    mc:Ignorable="d"
    Title="MainWindow" Height="250" Width="200">
    <!--资源通常定义在根节点,或者资源字典中-->
    <Window.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="HorizontalAlignment" Value="Center" />
            <Setter Property="FontFamily" Value="Comic Sans MS"/>
            <Setter Property="FontSize" Value="14"/>
        </Style>

        <!--通过BaseOn实现继承样式(Style)-->
        <Style BasedOn="{StaticResource {x:Type TextBlock}}"
            TargetType="TextBlock"
            x:Key="TitleText">
            <Setter Property="FontSize" Value="26"/>
            <Setter Property="Foreground">
                <Setter.Value>
                    <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                        <LinearGradientBrush.GradientStops>
                            <GradientStop Offset="0.0" Color="#90DDDD" />
                            <GradientStop Offset="1.0" Color="#5BFFFF" />
                        </LinearGradientBrush.GradientStops>
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    
    <Grid>
        <StackPanel VerticalAlignment="Top">
            <TextBlock Style="{StaticResource TitleText}" Name="textblock1">Title Text</TextBlock>
            <TextBlock>BaseOn TextBlock</TextBlock>
        </StackPanel>
    </Grid>
</Window>

效果

1111.png

评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容