WPF默认控件模板的获取和资源词典的使用

2022-10-17,,,,

一、获取默认控件模板

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}"/>

 

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

 

《WPF默认控件模板的获取和资源词典的使用.doc》

下载本文的Word格式文档,以方便收藏与打印。