愤怒者系列(一) WPF引用样式文件

本文介绍WPF中样式资源文件的引用方式,包括程序内和页面内的引用,并通过实例演示如何切换不同样式文件以实现主题变化。

今天我们说一下wpf引用样式资源文件。

wpf的样式控制分为行内,页内,和样式文件。而样式资源文件又分为程序内的样式文件和页内的样式文件。

我们今天讲的是引用的样式文件,因为可以切换样式文件来改变主题。

如果一个wpf程序同时有程序内的样式文件和页内文件,默认情况下是会匹配到页内样式文件的,同理,行内的优先级更高。

例子

例子里面我们程序内是引用Style.xaml,页面内是引用SimpleStyle.xaml来做比较。

创建样式文件

首先我们添加一个wpf的资源词典的xaml,我们改名字叫做Style.xaml。然后里面写上样式。

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:WpfStyleTest">
    <Style TargetType="{x:Type Button}">
        <Setter Property="Height" Value="25"></Setter>
        <Setter Property="Width" Value="200"></Setter>
        <Setter Property="Foreground" Value="#FF00FF23"/>
        <Setter Property="Background" Value="#FFBFAF0C"/>
    </Style>
    <Style x:Key="BStyle" TargetType="{x:Type Button}">
        <Setter Property="Height" Value="25"></Setter>
        <Setter Property="Width" Value="200"></Setter>
        <Setter Property="Foreground" Value="#FFBFAF0C"/>
        <Setter Property="Background" Value="#FF00FF23"/>
    </Style>
</ResourceDictionary>

备注:这里的TargetType是用来匹配控件类型的,如果引入了样式文件就会控制这个类型的所有样式,如果有个别按钮需要特别样式,我们可以使用key来指定使用特别的样式。

下面是SimpleStyle.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:WpfStyleTest">
    <Style x:Key="simBut" TargetType="{x:Type Button}">
        <Setter Property="Height" Value="25"></Setter>
        <Setter Property="Width" Value="200"></Setter>
        <Setter Property="Foreground" Value="#FF00FF23"/>
        <Setter Property="Background" Value="Red"/>
    </Style>
</ResourceDictionary>

程序内引用

然后我们修改App.xaml引用资源文件

<Application x:Class="WpfStyleTest.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfStyleTest"
             StartupUri="MainWindow.xaml"> 
    
    <Application.Resources>
        <!--引用外部资源-->
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Style.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>

</Application> 

页面内引用

<Window x:Class="WpfStyleTest.MainWindow"
        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:WpfStyleTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525"> 
    <!--引用外部资源-->
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="SimpleStyle.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
</Window>
备注:页面内引用和程序内引用是可以同时引用的,引用了两个不同的样式的情况下,默认是匹配页面内的样式引用,指定key是可以指定页面内和程序内的样式。

使用

<Window x:Class="WpfStyleTest.MainWindow"
        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:WpfStyleTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525"> 
    <!--引用外部资源-->
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="SimpleStyle.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <StackPanel Orientation="Vertical">
            <Button x:Name="button" Style="{StaticResource simBut}" Content="窗口的外部资源,指定样式" /> 
            <Button x:Name="button2" Style="{StaticResource BStyle}" Content="程序的外部资源,指定样式" />
            <Button x:Name="button3" Content="程序的外部资源,默认样式" />
        </StackPanel>
    </Grid>
</Window>

备注:指定key的是时候写上 Style="{StaticResource simBut}"对应的key就行了。

这里使用的时候如果加上一个页面的外部资源,默认样式的情况下,程序内的外部资源,默认样式就不起效果了。









评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值