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

来源:互联网 发布:淘宝透明睡衣买家秀 编辑:程序博客网 时间:2024/05/16 05:58

今天我们说一下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就行了。

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









1 0
原创粉丝点击