zoukankan      html  css  js  c++  java
  • WPF默认控件模板的获取和资源词典的使用

    一、获取默认的控件模板

    WPF修改控件模板是修改外观最方便的方式,但是会出现不知道原来的控件的模板长什么样,或者想用来参考的,下面分享一下获取某控件默认控件模板的方式(已Button为例):

    1、创建一个Button

    2、在界面上选择Button,右键->编辑模板->编辑副本 ,即可看到XAML中自动生成了原始的控件模板

    3、可以在默认模板上修改其中的一些属性运行测试是否生效

    这样在默认的控件模板上编辑,只修改需要修改的部分即可,可以大大减少工作量,也添加了容错率。但是会发现所有的模板和样式都放在主界面的XAML代码量会很多、很乱,所以可以采用单独的资源词典来存放这些模板和样式,主界面只要根据Key调用即可。

    二、资源字典的使用

    1、选中项目右键->添加->新建项->资源词典(WPF)

    生成的初始资源词典如下:

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:local="clr-namespace:TemplateDemo">
    </ResourceDictionary>

    现在可以在内容将模板和样式作为资源分流到各个资源词典,现在将Button的默认模板转移至该控件模板

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:local="clr-namespace:TemplateDemo">
        
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary>
                <Style x:Key="FocusVisual">
                    <Setter Property="Control.Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
                <SolidColorBrush x:Key="Button.Static.Background" Color="#FFDDDDDD"/>
                <SolidColorBrush x:Key="Button.Static.Border" Color="#FF707070"/>
                <SolidColorBrush x:Key="Button.MouseOver.Background" Color="#FFBEE6FD"/>
                <SolidColorBrush x:Key="Button.MouseOver.Border" Color="#FF3C7FB1"/>
                <SolidColorBrush x:Key="Button.Pressed.Background" Color="#FFC4E5F6"/>
                <SolidColorBrush x:Key="Button.Pressed.Border" Color="#FF2C628B"/>
                <SolidColorBrush x:Key="Button.Disabled.Background" Color="#FFF4F4F4"/>
                <SolidColorBrush x:Key="Button.Disabled.Border" Color="#FFADB2B5"/>
                <SolidColorBrush x:Key="Button.Disabled.Foreground" Color="#FF838383"/>
                <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
                    <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
                    <Setter Property="Background" Value="{StaticResource Button.Static.Background}"/>
                    <Setter Property="BorderBrush" Value="{StaticResource Button.Static.Border}"/>
                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
                    <Setter Property="BorderThickness" Value="1"/>
                    <Setter Property="HorizontalContentAlignment" Value="Center"/>
                    <Setter Property="VerticalContentAlignment" Value="Center"/>
                    <Setter Property="Padding" Value="1"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type Button}">
                                <StackPanel>
                                    <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
                                        <ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                                    </Border>
                                </StackPanel>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsDefaulted" Value="true">
                                        <Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                                    </Trigger>
                                    <Trigger Property="IsMouseOver" Value="true">
                                        <Setter Property="Background" TargetName="border" Value="{StaticResource Button.MouseOver.Background}"/>
                                        <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.MouseOver.Border}"/>
                                    </Trigger>
                                    <Trigger Property="IsPressed" Value="true">
                                        <Setter Property="Background" TargetName="border" Value="{StaticResource Button.Pressed.Background}"/>
                                        <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Pressed.Border}"/>
                                    </Trigger>
                                    <Trigger Property="IsEnabled" Value="false">
                                        <Setter Property="Background" TargetName="border" Value="{StaticResource Button.Disabled.Background}"/>
                                        <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Disabled.Border}"/>
                                        <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="{StaticResource Button.Disabled.Foreground}"/>
                                    </Trigger>
                                </ControlTemplate.Triggers>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

    2、要引用该资源字典还需要在App.Xaml中进行声明,我的名称叫TemplateDictionary.xaml,需要保证其命名空间一致

     <Application.Resources>
            <ResourceDictionary Source="TemplateDictionary.xaml"></ResourceDictionary>
        </Application.Resources>

    3、在主XAML中使用StaticResource或DynamicResource进行静态或动态引用即可

    <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="309,286,0,0" VerticalAlignment="Top" Width="75" Style="{StaticResource ButtonStyle1}"/>

    以上就是关于获取默认空间模板和使用资源词典的一些简单的介绍,结合起来使用可以搭建简洁方便的代码布局

  • 相关阅读:
    【IT笔试面试题整理】字符串的组合
    【IT笔试面试题整理】字符串的排列
    进程与线程的区别?转
    【IT笔试面试题整理】判断一个树是否是另一个的子树
    【IT笔试面试题整理】连续子数组的最大和
    【IT笔试面试题整理】反转链表
    【IT笔试面试题整理】不用加减乘除做加法
    java Singleton 几种方式解析转
    【IT笔试面试题整理】判断链表是否存在环路,并找出回路起点
    【IT笔试面试题整理】删除无序链表中重复的节点
  • 原文地址:https://www.cnblogs.com/xiaomengshan/p/11446436.html
Copyright © 2011-2022 走看看