WPF 中,如何使用自定义的resources

第一步,先自己自定义一个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>

时间: 2024-10-14 20:22:00

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

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

原文:WPF中使用WindowChrome自定义窗口中遇到的最大化问题 FrameWork 4.5 之后,内置了WindowChrome类,官方文档: https://msdn.microsoft.com/en-us/library/system.windows.shell.windowchrome.aspx 如果你是旧版本,可以去搜索单独的dll. 上方的页面,解释和例子也都是旧版本的,如果新版本,比如和我一样,是4.6.2,可以继续往下看新的用法,相比文档中的旧方式,新的用法的确更简单有效了

WPF新手之如何自定义TreeView点击后的背景色

转载自csdn:WPF新手之如何自定义TreeView点击后的背景色 其它控件也同样适用: 对于一时找不出好办法的情况,直接用StyleSnooper找到所需的控件,查看它的默认Style.然后找到所需的设置,如这里是找到 [xhtml] view plaincopy 1 <Trigger Property="TreeViewItem.IsSelected"> 2 <Setter Property="Panel.Background" Target

WPF中自定义MarkupExtension

原文:WPF中自定义MarkupExtension 在介绍这一篇文章之前,我们首先来回顾一下WPF中的一些基础的概念,首先当然是XAML了,XAML全称是Extensible Application Markup Language (可扩展应用程序标记语言),是专门用于WPF技术中的UI设计语言,通过使用XAML语言,我们能够快速设计软件界面,同时能够通过绑定这种机制能够很好地实现界面和实现逻辑之间的解耦,这个就是MVVM模式的核心了,那么今天我们介绍的MarkupExtension和XAML之

WPF中自定义绘制内容

先说结论:实现了在自定义大小的窗口中,加载图片,并在图片上绘制一个矩形框:且在窗口大小改变的情况,保持绘制的矩形框与图片的先对位置不变. 在WinForm中,我们可以很方便地绘制自己需要的内容,在WPF中似乎被限制了,不能够很方便的使用:然后需求有总是奇葩的,所以在这里简单地总结一下. 在WinForm中,如果需要自己绘制,就需要拿到Graphics对象:同样的,我们就希望在WPF也得到一个其同样作用的对象,这个对象就是DrawingContext类的实例对象. 具体来说,就是要重载 UIEle

【转】WPF中实现自定义虚拟容器(实现VirtualizingPanel)

在WPF应用程序开发过程中,大数据量的数据展现通常都要考虑性能问题.有下面一种常见的情况:原始数据源数据量很大,但是某一时刻数据容器中的可见元素个数是有限的,剩余大多数元素都处于不可见状态,如果一次性将所有的数据元素都渲染出来则会非常的消耗性能.因而可以考虑只渲染当前可视区域内的元素,当可视区域内的元素需要发生改变时,再渲染即将展现的元素,最后将不再需要展现的元素清除掉,这样可以大大提高性能.在WPF中System.Windows.Controls命名空间下的VirtualizingStackP

在VS2005中设置WPF中自定义按钮的事件

原文:在VS2005中设置WPF中自定义按钮的事件 上篇讲了如何在Blend中绘制圆角矩形(http://blog.csdn.net/johnsuna/archive/2007/08/13/1740781.aspx),本篇继续下一步骤,如何自定义按钮的事件. (1)首先,在VS2005中打开上篇所建的项目(File - Open Project),找到LinearGradientButton.csproj(这是我这里的项目名称),打开之后,双击LinearGradientDemo.xaml.cs

01.WPF中制作无边框窗体

[引用:]http://blog.csdn.net/johnsuna/article/details/1893319 众所周知,在WinForm中,如果要制作一个无边框窗体,可以将窗体的FormBorderStyle属性设置为None来完成.如果要制作成异形窗体,则需要使用图片或者使用GDI+自定义绘制. 那么,在WPF中,我们怎样制作一个无边框窗体呢? 答案是将Window的WindowStyle属性设置为None,即WindowStyle="None" .如果是非矩形的异形窗体,则

WPF快速指导10:WPF中的事件及冒泡事件和隧道事件(预览事件)的区别

本文摘要: 1:什么是路由事件: 2:中断事件路由: 3:自定义路由事件: 4:为什么需要自定义路由事件: 5:什么是冒泡事件和预览事件(隧道事件): 1:什么是路由事件 WPF中的事件为路由事件,所谓路由事件,MSDN定义如下: 功能定义:路由事件是一种可以针对元素树中的多个侦听器(而不是仅针对引发该事件的对象)调用处理程序的事件. 实现定义:路由事件是一个 CLR 事件,可以由 RoutedEvent 类的实例提供支持并由 Windows Presentation Foundation (W

WPF中的数据模板(DataTemplate)(转)

原文地址 http://www.cnblogs.com/zhouyinhui/archive/2007/03/30/694388.html WPF中的数据模板(DataTemplate)                                                                                                                          周银辉 在WPF中我们可以为自己的数据定制显示方式,也就是说虽然某数据