第一步,先自己自定义一个Resources

1.新建一个xaml文件,在其中自定义好自己的Resources

这个Resource 的根节点是

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"></ResourceDictionary>

然后在根节点中写入自己要定义的样式,

注意,在对应特定控件的样式中,要写入  TargetType 对应于要设置样式的控件,如:

TargetType="{x:Type Button}",并且对样式写上x:key= 以便查找:

入下面一个例子:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <GradientStopCollection x:Key="MyGlassGradientStopsResource">
        <GradientStop Color="WhiteSmoke" Offset="0.2" />
        <GradientStop Color="Transparent" Offset="0.4" />
        <GradientStop Color="WhiteSmoke" Offset="0.5" />
        <GradientStop Color="Transparent" Offset="0.75" />
        <GradientStop Color="WhiteSmoke" Offset="0.9" />
        <GradientStop Color="Transparent" Offset="1" />
    </GradientStopCollection>
    <LinearGradientBrush x:Key="MyGlassBrushResource"
   StartPoint="0,0" EndPoint="1,1" Opacity="0.75"
   GradientStops="{StaticResource MyGlassGradientStopsResource}" />
    <!-- Styles and other resources below here. -->

    <LinearGradientBrush x:Key="GrayBlueGradientBrush"
    StartPoint="0,0" EndPoint="1,1">
        <GradientStop Color="YellowGreen" Offset="0" />
        <GradientStop Color="#CCCCFF" Offset="0.5" />
        <GradientStop Color="YellowGreen" Offset="1" />
    </LinearGradientBrush>

   <Style x:Key="GlassButton" TargetType="{x:Type Button}">
        <Setter Property="Background"
      Value="{StaticResource GrayBlueGradientBrush}" />
        <Setter Property="Width" Value="80" />
        <Setter Property="Margin" Value="10" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid Width="{TemplateBinding Width}"
      Height="{TemplateBinding Height}" ClipToBounds="True">

                        <!-- Outer Rectangle with rounded corners. -->
                        <Rectangle x:Name="outerRectangle" HorizontalAlignment="Stretch"
      VerticalAlignment="Stretch" Stroke="{TemplateBinding Background}"
      RadiusX="20" RadiusY="20" StrokeThickness="5" Fill="Transparent" />

                        <!-- Inner Rectangle with rounded corners. -->
                        <Rectangle x:Name="innerRectangle" HorizontalAlignment="Stretch"
        VerticalAlignment="Stretch" Stroke="Transparent"
        StrokeThickness="20"
        Fill="{TemplateBinding Background}" RadiusX="20" RadiusY="20"
      />

                        <!-- Glass Rectangle -->
                        <Rectangle x:Name="glassCube" HorizontalAlignment="Stretch"
        VerticalAlignment="Stretch"
        StrokeThickness="2" RadiusX="10" RadiusY="10" Opacity="0"
        Fill="{StaticResource MyGlassBrushResource}"
        RenderTransformOrigin="0.5,0.5">
                            <Rectangle.Stroke>
                                <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
                                    <LinearGradientBrush.GradientStops>
                                        <GradientStop Offset="0.0" Color="LightBlue" />
                                        <GradientStop Offset="1.0" Color="Gray" />
                                    </LinearGradientBrush.GradientStops>
                                </LinearGradientBrush>
                            </Rectangle.Stroke>

                            <!-- These transforms have no effect as they
             are declared here.
             The reason the transforms are included is to be targets
             for animation (see later). -->
                            <Rectangle.RenderTransform>
                                <TransformGroup>
                                    <ScaleTransform />
                                    <RotateTransform />
                                </TransformGroup>
                            </Rectangle.RenderTransform>

                            <!-- A BevelBitmapEffect is applied to give the button a
               "Beveled" look. -->
                            <Rectangle.BitmapEffect>
                                <BevelBitmapEffect />
                            </Rectangle.BitmapEffect>
                        </Rectangle>

                        <!-- Present Text of the button. -->
                        <DockPanel Name="myContentPresenterDockPanel">
                            <ContentPresenter x:Name="myContentPresenter" Margin="20"
          Content="{TemplateBinding  Content}" TextBlock.Foreground="Black" />
                        </DockPanel>
                    </Grid>

                    <ControlTemplate.Triggers>
                        <!-- Set properties when mouse pointer is over the button. -->
                        <Trigger Property="IsMouseOver" Value="True">

                            <!-- Below are three property settings that occur when the
         condition is met (user mouses over button).  -->
                            <!-- Change the color of the outer rectangle when user          mouses over it. -->
                            <Setter Property ="Rectangle.Stroke" TargetName="outerRectangle"
      Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />

                            <!-- Sets the glass opacity to 1, therefore, the          glass "appears" when user mouses over it. -->
                            <Setter Property="Rectangle.Opacity" Value="1"       TargetName="glassCube" />

                            <!-- Makes the text slightly blurry as though you were          looking at it through blurry glass. -->
                            <Setter Property="ContentPresenter.BitmapEffect"       TargetName="myContentPresenter">
                                <Setter.Value>
                                    <BlurBitmapEffect Radius="1" />
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        <!-- Set properties when button has focus. -->
                        <Trigger Property="IsFocused" Value="true">
                            <Setter Property="Rectangle.Opacity" Value="1"       TargetName="glassCube" />
                            <Setter Property="Rectangle.Stroke" TargetName="outerRectangle"
      Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
                            <Setter Property="Rectangle.Opacity" Value="1" TargetName="glassCube" />
                        </Trigger>

                        <!-- Animations that start when mouse enters and leaves button. -->
                        <EventTrigger RoutedEvent="Mouse.MouseEnter">
                            <EventTrigger.Actions>
                                <BeginStoryboard Name="mouseEnterBeginStoryboard">
                                    <Storyboard>

                                        <!-- This animation makes the glass rectangle shrink in the X direction. -->
                                        <DoubleAnimation Storyboard.TargetName="glassCube"
          Storyboard.TargetProperty=
          "(Rectangle.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"
          By="-0.1" Duration="0:0:0.5" />

                                        <!-- This animation makes the glass rectangle shrink in the Y direction. -->
                                        <DoubleAnimation
        Storyboard.TargetName="glassCube"
          Storyboard.TargetProperty=
          "(Rectangle.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"
          By="-0.1" Duration="0:0:0.5" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger.Actions>
                        </EventTrigger>
                        <EventTrigger RoutedEvent="Mouse.MouseLeave">
                            <EventTrigger.Actions>

                                <!-- Stopping the storyboard sets all animated properties back to default. -->
                                <StopStoryboard BeginStoryboardName="mouseEnterBeginStoryboard" />
                            </EventTrigger.Actions>
                        </EventTrigger>

                        <!-- Animation fires when button is clicked, causing glass to spin.  -->
                        <EventTrigger RoutedEvent="Button.Click">
                            <EventTrigger.Actions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <DoubleAnimation Storyboard.TargetName="glassCube"
          Storyboard.TargetProperty=
          "(Rectangle.RenderTransform).(TransformGroup.Children)[1].(RotateTransform.Angle)"
          By="360" Duration="0:0:0.5" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger.Actions>
                        </EventTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>

    </Style>
</ResourceDictionary>

第二步:在项目的配置文件 App.xaml 中配置 自定义Resources

在 App.xml根节点下面建立一个 <Application.Resources>,其中定义ResourceDictionary节点

在其中配置自己定义的Resources,例如:

<Application x:Class="PaperOfficerPro.App"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary  Source="Resource/ButtonResource.xaml"></ResourceDictionary>
                <ResourceDictionary  Source="Resource/ExpanderStyleResource.xaml"></ResourceDictionary>
                <ResourceDictionary  Source="Resource/GlassButtonStyle.xaml"></ResourceDictionary>
                <ResourceDictionary  Source="Resource/DataGridResource.xaml"></ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

 

第三步,页面引用Resource中自定义的样式:

在页面的需要引用样式的控件的样式属性上引用样式

如需要在Button属性中引用第一步中定义的TargetType="{x:Type Button}" 的样式,那么就引用这个样式的x:key中的值。

例如:

<Button x:Name="LoginSubmit" Style="{DynamicResource GlassButton}" Width="Auto" Margin="117,271,148,20" Content="确定登陆" Click="LoginSubmit_Click" FontSize="14" Grid.RowSpan="2" >
        </Button>

WPF 中,如何使用自定义的resources的更多相关文章

  1. WPF中使用WindowChrome自定义窗口中遇到的最大化问题

    FrameWork 4.5 之后,内置了WindowChrome类,官方文档: https://msdn.microsoft.com/en-us/library/system.windows.shel ...

  2. WPF中ToolTip的自定义

    ToolTip或者PopUp这个控件在做界面时会经常用到.如何对ToolTip进行自定义呢? 1.首先自定义tooltip的controlTemplate,完全清除系统默认效果, 如下:        ...

  3. 在WPF中自定义你的绘制(一)

    原文:在WPF中自定义你的绘制(一)   在WPF中自定义你的绘制(一)                                                                 ...

  4. WPF中自定义MarkupExtension

    在介绍这一篇文章之前,我们首先来回顾一下WPF中的一些基础的概念,首先当然是XAML了,XAML全称是Extensible Application Markup Language (可扩展应用程序标记 ...

  5. WPF中自定义的DataTemplate中的控件,在Window_Loaded事件中加载机制初探

    原文:WPF中自定义的DataTemplate中的控件,在Window_Loaded事件中加载机制初探         最近因为项目需要,开始学习如何使用WPF开发桌面程序.使用WPF一段时间之后,感 ...

  6. WPF中实现自定义虚拟容器(实现VirtualizingPanel)

    WPF中实现自定义虚拟容器(实现VirtualizingPanel) 在WPF应用程序开发过程中,大数据量的数据展现通常都要考虑性能问题.有下面一种常见的情况:原始数据源数据量很大,但是某一时刻数据容 ...

  7. WPF中Image控件绑定到自定义类属性

    首先我们定义一个Student类,有ID,Name,Photo(保存图片路径). using System; using System.Collections.Generic; using Syste ...

  8. 在WPF中自定义你的绘制(五)

    原文:在WPF中自定义你的绘制(五) 在WPF中自定义你的绘制(五)                                                                   ...

  9. 在WPF中自定义你的绘制(三)

    原文:在WPF中自定义你的绘制(三) 在WPF中自定义你的绘制(三)                                                                  ...

随机推荐

  1. 转载Sql 获取数据库所有表及其字段名称,类型,长度

    转载原地址 http://www.cnblogs.com/Fooo/archive/2009/08/27/1554769.html SELECT (case when a.colorder=1 the ...

  2. GPS两点的距离

    目前手头的一个项目要用到GPS地理定位信息,很自然的就需要知道两个地点之间的距离,于是上网找了一下,同样自然的就有一些算法贴出来..(网络真是帮了大忙,省得我再去翻几何书自己研究算法了!公式早都忘光了 ...

  3. GridView控件显示图片

    与图片的二进制数据库存储和显示 1.将图片以二进制存入数据库 2.读取二进制图片在页面显示 3.设置Image控件显示从数据库中读出的二进制图片 4.GridView中ImageField以URL方式 ...

  4. java的socket 编程

    之前在学校的时候,有时间但是总是学不进去,现在工作了,总想多学点东西,但是又是没有时间来学习,只能在工作的闲暇之余,给自己充充电,随着学习的深入,越来月发现Java真的很强大,几乎什么都可以做的,以下 ...

  5. XMPP——Smack[2]会话、消息监听、字体表情和聊天窗口控制

    连接之后,拿到了connection,通过它可以搞定会话 建立一个会话 MessageListener msgListener = new MessageListener() { public voi ...

  6. Myeclipse:No projects are available for deployment to this server!

    这是因为以前的项目不是用myEclipse创建的,所以用myeclipse deploy的时候找不到你的项目. 可以这样做:右击原项目名 - myeclipse - Add myeclipse  We ...

  7. WINAPI 变量(2861个)

    WINAPI 变量(2861个)   这是从 c:\Program Files\Windows Kits\8.1\Include\um\WinUser.h 这个文件 中提取的 CTRL+F 查看变量所 ...

  8. 配置Ubuntu开发环境

    前言 新买了一台ThinkPad E431,主要看中了硬盘500G和7200转/分钟的速度,因此准备从x220上把工作环境迁移到新买的笔记本上. 为什么不要公司的电脑,是由于160G的ssd硬盘实在是 ...

  9. joa-framework 工作流高速开发框架(jeecg官方工作流版本号) 公布

    -------------------------------------- version:  joa-framework1.0.0.beta 版本号: JOA 工作流高速开发框架   Date:  ...

  10. leetcode第一刷_Jump Game

    这个题事实上非常easy的,我一開始想复杂了,它没要求记录路径,事实上仅仅要看一下每一步之后所能延伸到的最远的位置就能够了,在这一个最远位置前面的那些位置,都是能够到达的,假设扫到了某个i,它大于当前 ...