欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c# WPF中CheckBox样式的使用总结

程序员文章站 2022-03-08 21:41:40
背景  很多时候我们使用wpf开发界面的时候经常会用到各种空间,很多时候我们需要去自定义控件的样式来替换默认的样式,今天通过两个方法来替换wpf中的checkbox样式,透过这两个例子我们可以掌握基本...

背景

  很多时候我们使用wpf开发界面的时候经常会用到各种空间,很多时候我们需要去自定义控件的样式来替换默认的样式,今天通过两个方法来替换wpf中的checkbox样式,透过这两个例子我们可以掌握基本的wpf样式的开发如何定义controltemplate以及使用附加属性来为我们的控件增加新的样式。

常规使用

  我们在使用checkbox的时候,原始的样式有时不能满足我们的需求,这是我们就需要更改其模板,比如我们常用的一种,在播放器中“播放”、“暂停”按钮,其实这也是一种checkbox,只不过我们只是修改了其相关的模板罢了,下面贴出相关代码: 

<checkbox.style>
  <style targettype="{x:type checkbox}">
    <setter property="focusable" value="false" />
    <setter property="istabstop" value="false" />
    <!--把overridesdefaultstyle设置为true,表示这个控件不使用当前themes的任何属性。-->
    <setter property="overridesdefaultstyle" value="true" />
      <style.triggers>
        <trigger property="ischecked" value="true">
       <setter property="template">
          <setter.value>
            <controltemplate targettype="{x:type checkbox}">
              <grid background="transparent">
                   <image source="/earthsimulation;component/images/按钮-播放.png"/>
              </grid>
            </controltemplate>
         </setter.value>
      </setter>
      </trigger>
      <trigger property="ischecked" value="false">
       <setter property="template">
         <setter.value>
            <controltemplate targettype="{x:type checkbox}">
                <grid background="transparent">
                        <image source="/earthsimulation;component/images/按钮-暂停.png"/>
                 </grid>
            </controltemplate>
          </setter.value>
        </setter>
        </trigger>
      </style.triggers>
    </style>
  </checkbox.style>
</checkbox>  

进阶用法

  上面的使用较为简单,下面我们通过一个更加复杂一些的例子来增加对自定义控件模板的理解,我们先来看看我们定义的样式。

<style x:key="checkboxstyle" targettype="{x:type checkbox}">
               <setter property="focusvisualstyle" value="{x:null}"/>
               <setter property="background" value="transparent"/>
               <setter property="borderbrush" value="transparent"/>
               <setter property="foreground" value="{dynamicresource {x:static systemcolors.controltextbrushkey}}"/>
               <setter property="template">
                   <setter.value>
                       <controltemplate targettype="{x:type checkbox}">
                           <border x:name="templateroot" background="{templatebinding background}" >
                               <grid snapstodevicepixels="true">
                                   <grid.resources>
                                       <pathgeometry x:key="geometrycheck" figures="m540.5696 102.4c-225.83296 0-409.6 183.74656-409.6 409.6s183.76704 409.6 409.6 409.6c225.87392 0 409.6-183.74656 409.6-409.6s766.44352 102.4 540.5696 102.4zm721.16224 468.48l-175.37024 175.39072c-12.20608 12.1856-28.20096 18.28864-44.19584 18.28864-15.95392 0-31.96928-6.10304-44.15488-18.28864l-97.44384-97.44384c-24.39168-24.39168-24.39168-63.93856 0-88.33024 24.39168-24.41216 63.91808-24.41216 88.35072 0l53.248 53.248 131.23584-131.21536c24.35072-24.3712 63.95904-24.3712 88.33024 0c745.55392 404.52096 745.55392 444.08832 721.16224 468.48z"/>
                                   </grid.resources>
                                   <grid.columndefinitions>
                                       <columndefinition width="20"/>
                                       <columndefinition width="*"/>
                                       <columndefinition width="30"/>
                                   </grid.columndefinitions>
 
                                   <border>
                                       <path width="18" height="18" stretch="uniform"
                                             data="{binding (local_ctrl:geometryap.icongeometry), relativesource={relativesource mode=findancestor,ancestortype={x:type checkbox}}}"
                                             fill="{binding (local_ctrl:geometryap.iconfill), relativesource={relativesource mode=findancestor,ancestortype={x:type checkbox}}}"/>
                                   </border>
                                   <contentpresenter x:name="contentpresenter" grid.column="1" focusable="false"
                                                 margin="{templatebinding padding}"
                                                 recognizesaccesskey="true"
                                                 snapstodevicepixels="{templatebinding snapstodevicepixels}"
                                                 horizontalalignment="left"
                                                 verticalalignment="center"/>
 
                                   <border x:name="checkboxborder"
                                       grid.column="2"
                                       margin="1"
                                       borderbrush="transparent"
                                       borderthickness="1"
                                       background="transparent"
                                       horizontalalignment="left"
                                       verticalalignment="center">
 
                                       <grid x:name="markgrid">
                                           <path x:name="optionmark" width="20" height="20"
                                                 stretch="uniform"
                                                 data="{staticresource geometrycheck}"
                                                 fill="lightseagreen" margin="1" opacity="0"/>
                                       </grid>
                                   </border>
                               </grid>
                           </border>
                           <controltemplate.triggers>
                               <trigger property="hascontent" value="true">
                                   <setter property="padding" value="4,-1,0,0"/>
                               </trigger>
                               <trigger property="ismouseover" value="true">
                                   <setter property="cursor" value="hand"/>
                                   <setter property="background" value="#22222222"/>
                               </trigger>
                               <trigger property="isenabled" value="false">
                                   <setter property="opacity" value=".3"/>
                               </trigger>
                               <trigger property="ispressed" value="true">
                                   <setter property="cursor" value="hand"/>
                                   <setter property="background" value="#22333333"/>
                               </trigger>
                               <trigger property="ischecked" value="true">
                                   <setter property="opacity" targetname="optionmark" value="1"/>
                               </trigger>                               
                           </controltemplate.triggers>
                       </controltemplate>
                   </setter.value>
               </setter>
           </style>

  后面我们再来看看,我们使用checkbox的地方。

<checkbox grid.row="1" grid.column="1"
                                      ischecked="{binding vm.ishorizontal,elementname=_this}" content="horizontal"
                                      style="{staticresource checkboxstyle}"
                                      local_ctrl:geometryap.icongeometry="{staticresource geometrydirection}"
                                      local_ctrl:geometryap.iconfill="lightseagreen"/>

  这个地方我们为checkbox增加了两个附加属性icongeometry、iconfill这样我们就能够将这两个附加属性绑定到checkbox样式中的path里面的data和fill依赖项属性上面,通过上面的过程我们就能够定义各种各样的checkbox样式了,下面我们看看我们定义的这两个附加属性具体的代码。

public class geometryap : dependencyobject
{
    public static pathgeometry geticongeometry(dependencyobject obj)
    {
        return (pathgeometry)obj.getvalue(icongeometryproperty);
    }
    public static void seticongeometry(dependencyobject obj, pathgeometry value)
    {
        obj.setvalue(icongeometryproperty, value);
    }
 
    public static brush geticonfill(dependencyobject obj)
    {
        return (brush)obj.getvalue(iconfillproperty);
    }
    public static void seticonfill(dependencyobject obj, brush brush)
    {
        obj.setvalue(iconfillproperty, brush);
    }
 
    public static readonly dependencyproperty icongeometryproperty = dependencyproperty.registerattached("icongeometry", typeof(pathgeometry), typeof(geometryap));
    public static readonly dependencyproperty iconfillproperty = dependencyproperty.registerattached("iconfill", typeof(brush), typeof(geometryap), new propertymetadata(brushes.transparent));
}

  样式欣赏

c# WPF中CheckBox样式的使用总结

以上就是c# wpf中checkbox样式的使用总结的详细内容,更多关于c# wpf中checkbox样式的资料请关注其它相关文章!