把风格定格定义在单独的文件中

书名:WPF专业编程指南
作者:李应保
出版社:电子工业出版社
出版时间:2010-01
ISBN:9787121100116


一、把风格定格定义在单独的文件中

  • 前面举的例子,风格只施加到一两个控件上,即使不用风格,而直接在控件里设定相关属性值,也不是什么大不了的事。
    风格真正强大的功能是在大型软件工程中,这时你要开发多个WPF应用程序或DLL,如何让你的应用软件有一致的外观?
    近年骨架和皮肤(Themes/Skin)之所以流行,正是为满足这一要求。
    WPF并不支持Themes和Skin,但通过风格,可以很容易地实现Skin。

  • 这时,需要把风格定义在单独的文件中。第18章定义了多个风格文件,如OfficeBlueSkin.xaml,在该文件中定义了各种控件的外观,使得控件和Office 2007的蓝皮肤一样:

  <ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:RibbonLib="clr-namespace:Yingbao.Chapter18.RibbonLib"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  ....
    <SolidColorBrush x:Key="{ComponentResourceKey RibbonLib:Skins,
  PopupContainerBgBrush}" Color="White"/>
    <!--Specifies the chrome for the expanded RibbonGroup:-->
    <Style TargetType="{x:Type RibbonLib:RibbonChrome}"
  x:Key="{ComponentResourceKey RibbonLib:Skins,
  ExpandedRibbonGroupChromeStyle}">
        <Setter Property="InnerBorderThickness" Value="0.5"/>
        <Setter Property="NoAnimation" Value="False"/>
        <Setter Property="RenderFlat" Value="False"/>
        <Setter Property="Background" Value="{DynamicResource
  {ComponentResourceKey RibbonLib:Skins, RibbonPanelBgBrush}}"/>
        <Setter Property="CornerRadius" Value="3"/>
        <Setter Property="BorderBrush" Value="#80CADDF1"/>
        <Setter Property="MouseOverBackground" Value="{DynamicResource
  {ComponentResourceKey RibbonLib:Skins, RibbonPanelMouseOverBg}}"/>
    </Style>
    <!--Specifies the chrome for the collapsed RibbonGroup:-->
    <Style TargetType="{x:Type RibbonLib:RibbonChrome}"
  x:Key="{ComponentResourceKey RibbonLib:Skins,
  CollapsedRibbonGroupChromeStyle}">
        <Setter Property="RenderFlat " Value="False"/>
        <Setter Property="NoAnimation " Value="True"/>
        <Setter Property="BorderBrush" Value="{x:Null}"/>
        <Setter Property="CornerRadius" Value="4"/>
        <Setter Property="BorderBrush" Value="#80CADDF1"/>
        <Setter Property="Background" Value="{DynamicResource
  {ComponentResourceKey RibbonLib:Skins, MinimizedRibbonPanelBgBrush}}"/>
        <Setter Property="MouseOverBackground" Value="{DynamicResource
  {ComponentResourceKey RibbonLib:Skins, MinimizedMouseOverBrush}}"/>
        <Setter Property="MousePressedBackground"
  Value="{DynamicResource     {ComponentResourceKey RibbonLib:Skins,
  DefaultMousePressedBtnBrush}}"/>
    </Style>
  ......
  </ResourceDictionary>
  • 当需要使用Office 2007蓝皮肤的时候,只要在ResourceDictionary里加入该皮肤文件即可:
  <ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:RibbonLib="clr-namespace:Yingbao.Chapter18.RibbonLib"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary
  Source="pack://application:,,,/YBRibbonLib;Component/Themes/OfficeBlueS
  kin.xaml"/>
    </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
  • 这是组织大型软件Style的基本方法。

二、在FrameworkContentElement中使用风格

  • 风格不仅可以用在UI元素上,而且可以用在文档上。
    FlowDocument、FixedDocument、TextElement等都是从FrameworkContentElement中派生出来的,FrameworkContentElement中有一个Style属性,可以在这个属性中引入风格。
    下面的XAML中在Paragraph中使用了风格:
  <Window
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="StyleWithTargetType" Height="300" Width="300">
    <Window.Resources>
        <Style TargetType="{x:Type Paragraph}" x:Key="BluePara">
            <Setter Property ="Control.Foreground" Value ="Blue" />
            <Setter Property ="Control.FontSize" Value ="24" />
            <Setter Property ="Control.FontFamily" Value ="Times New
                Roman" />
            <Setter Property ="Control.FontWeight" Value ="Regular" />
        </Style>
        <Style TargetType="{x:Type Paragraph}" x:Key="RedPara">
            <Setter Property ="Control.Foreground" Value ="Red" />
            <Setter Property ="Control.FontSize" Value ="24" />
            <Setter Property ="Control.FontFamily" Value ="Times New
              Roman" />
            <Setter Property ="Control.FontWeight" Value ="Regular" />
        </Style>
    </Window.Resources>
    <StackPanel>
        <FlowDocumentPageViewer>
            <FlowDocument>
              <Paragraph Style="{StaticResource BluePara}">旅望因高尽
                </Paragraph>
              <Paragraph Style="{StaticResource RedPara}">乡心遇物悲
                </Paragraph>
            </FlowDocument>
        </FlowDocumentPageViewer>
      </StackPanel>
  </Window>
  • 这里“旅望因高尽”、“乡心遇物悲”两句分别使用两个不同的风格。
    风格的实际效果如图9-15所示。


    图9-15 在Document中使用风格
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容