原文:WPF把CheckBox的文字放到左边,开关在右边

效果

实现

这篇文章给了一个不错的参考方案.

http://www.codeproject.com/Articles/19141/WPF-Checkbox-Text-On-Left-Side

但是因为只是想把开关文字换一个位置,我觉得应该有CheckBox默认的模板再稍作修改就可以了.而且上面那个文章的方案在对其上面和原来的CheckBox有所不同.

所以我去看了一下CheckBox默认的模板.以下是和今天内容相关的部分.

<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate TargetType="CheckBox">
<BulletDecorator Background="#00FFFFFF" SnapsToDevicePixels="True">
<BulletDecorator.Bullet>
<mwt:BulletChrome Background="{TemplateBinding Panel.Background}" BorderBrush="{TemplateBinding Border.BorderBrush}" RenderMouseOver="{TemplateBinding UIElement.IsMouseOver}" RenderPressed="{TemplateBinding ButtonBase.IsPressed}" IsChecked="{TemplateBinding ToggleButton.IsChecked}" />
</BulletDecorator.Bullet>
<ContentPresenter RecognizesAccessKey="True" Content="{TemplateBinding ContentControl.Content}" ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" Margin="{TemplateBinding Control.Padding}" HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
</BulletDecorator>
</ControlTemplate>
</Setter.Value>
</Setter>

这里有一个很重要的类就是BulletDecorator,这是一个Decorator元素,它具有两个内容属性: Bullet 和 Child。 Bullet 属性定义用作项目符号的 UIElement。 Child 属性定义以可视化方式与该项目符号对齐的 UIElement。 也就是两个东西,左边的是Bullet右边的那个是Child.他们会自动对齐.所以解决方案就是把Bullet和Child的内容对换一下就好了.这里需要注意的是,BulletChrome是在clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero的程序集中.所以项目需要添加PresentationFramework.Aero.dll的引用,这个文件在C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0下面.而且需要在XAML文档中声明.

下面给我修改后的源文件.

<Window x:Class="TreeAndCheckBox.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary>
<Style TargetType="{x:Type CheckBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type CheckBox}">
<BulletDecorator SnapsToDevicePixels="True" Background="#00FFFFFF">
<BulletDecorator.Bullet>
<ContentPresenter
SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"
Margin="{TemplateBinding Control.Padding}"
VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}"
HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}"
ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}"
ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}"
Content="{TemplateBinding ContentControl.Content}"
RecognizesAccessKey="True"/>
</BulletDecorator.Bullet>
<Microsoft_Windows_Themes:BulletChrome
Background="{TemplateBinding Panel.Background}"
IsChecked="{TemplateBinding ToggleButton.IsChecked}"
RenderPressed="{TemplateBinding ButtonBase.IsPressed}"
RenderMouseOver="{TemplateBinding UIElement.IsMouseOver}"
BorderBrush="{TemplateBinding Border.BorderBrush}"/>
</BulletDecorator>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</Window.Resources> <Grid>
<CheckBox Content="测试CheckBox" HorizontalAlignment="Left" VerticalAlignment="Top" />
</Grid>
</Window>

WPF把CheckBox的文字放到左边,开关在右边的更多相关文章

  1. WPF笔记(2.7 文字布局)——Layout

    原文:WPF笔记(2.7 文字布局)--Layout 这一节介绍的是文字布局的几个控件:1.TextBlock      最基本的文字控件可以配置5个Font属性.TextWraping属性,&quo ...

  2. WPF:CheckBox竖向的滑块效果

    原文:WPF:CheckBox竖向的滑块效果 之前做了一个横向的滑块效果,<WPF:CheckBox滑块效果>,其实我觉得那个不好看,今天又做了一个竖向的玩. <Style Targ ...

  3. WPF系列-CheckBox

    自定义样式1 效果: 代码: <!-- CheckBox的样式 --> <Style TargetType="{x:Type CheckBox}"> < ...

  4. WPF 带CheckBox、图标的TreeView

    WPF 带CheckBox.图标的TreeView 在WPF实际项目开发的时候,经常会用到带CheckBox的TreeView,虽然微软在WPF的TreeView中没有提供该功能,但是微软在WPF中提 ...

  5. radio 和checkbox与文字对齐问题

    今天在项目中遇到radio和文字对齐问题(ie不明显,火狐和google比较明显),在此记录. 1.浏览器默认文字大小为14px,因而当文字字体为14px时radio和checkbox与文字对齐良好, ...

  6. WPF中CheckBox三种状态打勾打叉和滑动效果

    本文分为两个demo, 第一个demo实现checkBox的打叉或打勾的效果: 第二个demo部分实现checkBox的滑动效果. Demo1: wpf的CheckBox支持三种状态,但是美中不足的是 ...

  7. js文字从左边飞入效果

    贴代码之前,我们先讲一下它的原理,我们使用setInterval,让文字一开始置于屏幕看不到的位置,左右上下都可以,然后让它的位置不断移入到屏幕看得到的位置. 下面上代码: html: <h2 ...

  8. WPF 中的三维文字[转贴]

    原文:WPF 中的三维文字[转贴] 原文: http://msdn.microsoft.com/msdnmag/issues/07/10/Foundations/default.aspx?loc=zh ...

  9. checkbox与文字对齐

    checkbox和后面的文字如果不加样式,会导致checkbox和文字对齐,解决办法是对checkbox和文字同时添加如下样式: vertical-align:middle;

随机推荐

  1. js中top、clientTop、scrollTop、offsetTop的区别 文字详细说明版

    网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.clientHeight;网页可见区域宽: document.body.offset ...

  2. hdu1852 Beijing 2008

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1852 题目大意: 求2008^n的所有因子和m对k取余,然后求2008^m对k取余. 解题思路: 首 ...

  3. NW.js开发环境的搭建

    写在前面: 之前一直在找关于在mac怎么搭建nw.js的开发环境,苦于自己也没有很深入的理解,其实看看官方文档就差不多知道mac下要怎么整了. 官方文档的图: 正题开始: 先去下载一个nw.js的安装 ...

  4. 转载 【MySql】Update批量更新与批量更新多条记录的不同值实现方法

    批量更新 mysql更新语句很简单,更新一条数据的某个字段,一般这样写: UPDATE mytable SET myfield = 'value' WHERE other_field = 'other ...

  5. serlvet配置xml和@WebServlet

    简单介绍 XML元素不仅是大小写敏感的,而且它们还对出现在其他元素中的次序敏感.例如,XML头必须是文件中的第一项,DOCTYPE声明必须是第二项,而web-app元素必须是第三项.在web-app元 ...

  6. Android学习笔记_44_apk安装、反编译及防治反编译

    一.APK安装 1.首先需要AndroidManifest.xml中加入安装程序权限: <!-- 安装程序权限 --> <uses-permission android:name=& ...

  7. 消息中间件JMS(一)

    1.JMS入门 1.1消息中间件 模块之间的依赖也称之为耦合.而耦合越多,之后的维护工作就越困难.那么如果改善系统模块调用关系.减少模块之间的耦合呢?我们接下来就介绍一种解决方案----消息中间件. ...

  8. 第10章 使用STM32CubeMX新建工程

    STM32Cube是一项意法半导体的原创活动, 通过减少开发工作.时间和成本, 使开发者的开发工作更轻松.STM32Cube 是一个全面的软件平台,包括了ST产品的每个系列.(如,STM32CubeF ...

  9. neo4j 安装步骤 转自:http://blog.csdn.net/luoluowushengmimi/article/details/19987995

    1. Neo4j简介 Neo4j是一个用Java实现的.高性能的.NoSQL图形数据库.Neo4j 使用图(graph)相关的概念来描述数据模型,通过图中的节点和节点的关系来建模.Neo4j完全兼容A ...

  10. 关于改变placeholder的颜色

    input::-webkit-input-placeholder{ color:red; } input::-moz-placeholder{ /* Mozilla Firefox 19+ */ co ...