最近在看wpf相关东西,虽然有过两年的wpf方面的开发经验,但是当时开发的时候,许多东西一知半解,至今都是模模糊糊,框架基本是别人搭建,自己也就照着模板写写,现在许多东西慢慢的理解了,回顾以前的若干记忆,然后解决自己不懂的方面,在CSDN做记录,写下一些自己的理解,方便以后查阅:

    今天写的是关于wpf的资源字典里面做触发器,然后主界面进行调用:

   1: 首先建立了个wpf窗体程序WpfApplication1;工程里面自动生成App.xaml和MainWindow.xaml两个文件,这两个文件就不做介绍了,App.xaml会在下面用到。

    2:然后呢。一般都会将控件的触发器写成这样:

  <Button Name="btnUpdate" Grid.Row="1" Width="100" Height="30" Content="更 新"  Command="{Binding CmdCommand}" CommandParameter="{Binding Oberve}">
                <Button.Style>
                    <Style TargetType="{x:Type Button}">
                        <Style.Triggers>
                            <Trigger Property="Button.IsMouseOver" Value="True">
                                <Setter Property="Button.Foreground" Value="Blue"/>
                            </Trigger>
                            <Trigger Property="Button.IsPressed" Value="True">
                                <Setter Property="Button.Foreground" Value="Red"/>
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>
        </Button>

  那么控件多的时候,就显得臃肿了,于是就想到了【资源字典】,在资源字典里面写这些,然后调用,新建了【Dictionary1.xaml】文件

将要是实现的式样写进它里面:

  <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="CircleButtonStyle" TargetType="{x:Type Button}">
        <Setter Property="Border.BorderThickness" Value="1,1,1,1" />
        <Setter Property="Border.CornerRadius" Value="3" />
        <Setter Property="Height" Value="36" />
        <Setter Property="Width" Value="36" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid>
                        <Ellipse Fill="{TemplateBinding Background}"/>
                        <Ellipse>
                            <Ellipse.Fill>
                                <RadialGradientBrush>
                                    <GradientStop Offset="0" Color="#00000000"/>
                                    <GradientStop Offset="0.88" Color="#00000000"/>
                                    <GradientStop Offset="1" Color="#80000000"/>
                                </RadialGradientBrush>
                            </Ellipse.Fill>
                        </Ellipse>
                        <Ellipse Margin="10" x:Name="highlightCircle" >
                            <Ellipse.Fill >
                                <LinearGradientBrush >
                                    <GradientStop Offset="0" Color="#50FFFFFF"/>
                                    <GradientStop Offset="0.5" Color="#00FFFFFF"/>
                                    <GradientStop Offset="1" Color="#50FFFFFF"/>
                                </LinearGradientBrush>
                            </Ellipse.Fill>
                        </Ellipse>
                        <ContentPresenter x:Name="content" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="RenderTransform">
                        <Setter.Value>
                            <RotateTransform Angle="10"></RotateTransform>
                        </Setter.Value>
                    </Setter>
                    <Setter Property="RenderTransformOrigin" Value="0.5,0.5"></Setter>
                    <Setter Property="Background" Value="#FF0CC030" />
                </Trigger>
                <Trigger Property="IsPressed" Value="True">
                    <Setter Property="Foreground" Value="Red"/>
                </Trigger>
        </Style.Triggers>
    </Style>
</ResourceDictionary>

     3:这下就用到App.xaml发挥作用了;App这个文件是协助运行主窗口的文件,在app文件里面将数据资源里面的东东引进:

   <Application x:Class="WpfApplication1.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="Dictionary1.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

  4:这样后,MainWindow.xaml文件中就可以引进数据字典里面的式样和触发器了;

        <Grid>
        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="157,119,0,0" Name="button1"
  Style="{StaticResource CircleButtonStyle}"//引进数据字典里面的式样
                VerticalAlignment="Top" Width="75" />
    </Grid>

生成如下效果:

最新文章

  1. swiper 增加同页面增加2个滚动
  2. enum使用
  3. C# barcode生成代码
  4. c语言中用宏定义一个常量,数字后面带个U, L, F的含义
  5. 从new Function创建函数联想到MVC模式
  6. Team Homework #1 学长“学霸英语学习软件”试用
  7. map/reduce实现 排序
  8. javascript判断设备类型-手机(mobile)、安卓(android)、电脑(pc)、其他(ipad/iPod/Windows)等
  9. Python enumerate函数
  10. Java学习作业(14.4.21)
  11. 【转】【经典算法】——KMP,深入讲解next数组的求解
  12. C# var 隐式类型 var 用法 特点
  13. Hadoop2.0环境安装
  14. DUAL PORT RAM应用实例
  15. 整理:FPGA选型
  16. k8s sidecar, Ambassador, Adapter containers
  17. Oracle 学习之exists
  18. 【转】外国朋友出的js题目,你能对几道
  19. pdf转中文txt
  20. 【原】Coursera—Andrew Ng机器学习—Week 6 习题—Advice for applying machine learning

热门文章

  1. [Effective JavaScript 笔记]第24条:使用变量保存arguments对象
  2. [BZOJ1101][POI2007]Zap
  3. Struts2中通配符的使用
  4. HDU 1505 City Game (hdu1506 dp二维加强版)
  5. Shell脚本中cd命令使用
  6. Firefox上Web开发工具库一览
  7. 【SpringMVC】SpringMVC系列12之数据类型转换、格式化、校验
  8. cURL的几个经典实例
  9. Minimum Path Sum
  10. jsp回车键登录代码