.net WPF中复选框左侧的文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17465867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Text on the left side of checkbox in WPF?
提问by Ivan Peric
What is the easiest way to put checkbox content (text) on the left side of the checkbox itself?
将复选框内容(文本)放在复选框本身的左侧的最简单方法是什么?
回答by nmclean
A solution that maximizes "easiness" and "correctness" is to make a RightToLeftcheckbox with LeftToRightcontent:
最大化“易用性”和“正确性”的解决方案是创建一个RightToLeft包含LeftToRight内容的复选框:
<CheckBox FlowDirection="RightToLeft">
<TextBlock FlowDirection="LeftToRight" Text="CheckBox Content:" />
</CheckBox>
Or if you'd like a style:
或者,如果您想要一种风格:
<Style TargetType="CheckBox">
<Setter Property="FlowDirection" Value="RightToLeft" />
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<ContentControl FlowDirection="LeftToRight" Content="{Binding}" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
回答by Ivan Peric
In code:
在代码中:
System.Windows.Controls.CheckBox checkBox = new System.Windows.Controls.CheckBox();
checkBox.Content = ":CheckBox Enabled";
checkBox.FlowDirection = System.Windows.FlowDirection.RightToLeft;
In XAML:
在 XAML 中:
<CheckBox FlowDirection="RightToLeft" Content=":CheckBox Enabled" />
EDIT
编辑
User punker76helped me notice that colon ":" has to be places infrontof the text to be displayed correctly, at the end ("CheckBox Enabled:"), probably caused by an affect flow direction has on text element. Nice catch.
用户punker76帮我注意到,冒号“:”必须是地方盈文的正确显示,在年底(“复选框启用”),可能是由造成影响流动方向具有文本元素。不错的收获。
回答by punker76
Another way is to make a new custom style
另一种方法是制作新的自定义样式
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush Color="#F4F4F4"
x:Key="CheckBoxFillNormal" />
<SolidColorBrush Color="#8E8F8F"
x:Key="CheckBoxStroke" />
<Style x:Key="EmptyCheckBoxFocusVisual">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Margin="1"
SnapsToDevicePixels="true"
Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"
StrokeDashArray="1 2"
StrokeThickness="1" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="CheckRadioFocusVisual">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle Margin="14,0,0,0"
SnapsToDevicePixels="true"
Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"
StrokeDashArray="1 2"
StrokeThickness="1" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type CheckBox}"
x:Key="ContentLeftCheckBoxStyle">
<Setter Property="Foreground"
Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
<Setter Property="Background"
Value="{StaticResource CheckBoxFillNormal}" />
<Setter Property="BorderBrush"
Value="{StaticResource CheckBoxStroke}" />
<Setter Property="BorderThickness"
Value="1" />
<Setter Property="FocusVisualStyle"
Value="{StaticResource EmptyCheckBoxFocusVisual}" />
<Setter Property="VerticalContentAlignment"
Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type CheckBox}">
<StackPanel Orientation="Horizontal">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
<BulletDecorator Background="Transparent"
SnapsToDevicePixels="true"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
<BulletDecorator.Bullet>
<Microsoft_Windows_Themes:BulletChrome Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
IsChecked="{TemplateBinding IsChecked}"
RenderMouseOver="{TemplateBinding IsMouseOver}"
RenderPressed="{TemplateBinding IsPressed}" />
</BulletDecorator.Bullet>
</BulletDecorator>
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="HasContent"
Value="true">
<Setter Property="FocusVisualStyle"
Value="{StaticResource CheckRadioFocusVisual}" />
<Setter Property="Padding"
Value="0,0,4,0" />
</Trigger>
<Trigger Property="IsEnabled"
Value="false">
<Setter Property="Foreground"
Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
usage:
用法:
<CheckBox Style="{StaticResource ContentLeftCheckBoxStyle}" Content="CheckBox:" />


hope that helps!
希望有帮助!
回答by Bill Foster
Another workaround, to avoid the subtle problems discussed above with flow direction and the large amount of code when defining an entirely new CheckBox style, is to just use a WrapPanel containing a Label (with the desired CheckBox content string) and a CheckBox (with no content string).
另一种解决方法是在定义全新的 CheckBox 样式时避免上面讨论的流向和大量代码的微妙问题,是仅使用包含 Label(具有所需的 CheckBox 内容字符串)和 CheckBox(没有内容字符串)。
<WrapPanel>
<Label Content="Checkbox content"/>
<CheckBox VerticalAlignment="Center" Margin="5,0,0,0"/>
</WrapPanel>
回答by Lance
I spent two hours for it, but i found the best decision
我花了两个小时,但我找到了最好的决定
<Style x:Key="TextAlignLeft" TargetType="CheckBox">
<Style.Resources>
<Style TargetType="Path">
<Setter Property="FlowDirection" Value="LeftToRight" />
</Style>
<Style TargetType="TextBlock">
<Setter Property="FlowDirection" Value="LeftToRight" />
</Style>
</Style.Resources>
<Setter Property="FlowDirection" Value="RightToLeft" />
</Style>
回答by Akshatha
I know it's been a while and I'm late. But after going through several complicated answers and searching a lot on the internet I finally found the simplest way to achieve this without distorting the tick from here.
我知道已经有一段时间了,我迟到了。但是在经历了几个复杂的答案并在互联网上搜索了很多之后,我终于找到了最简单的方法来实现这一点,而不会扭曲这里的勾号。
<CheckBox Content="Checked" FlowDirection="RightToLeft">
<CheckBox.Resources>
<Style TargetType="{x:Type Path}">
<Setter Property="FlowDirection" Value="LeftToRight" />
</Style>
</CheckBox.Resources>
</CheckBox>
Result:
结果:
回答by John
easiest way is to use a stack panel:
最简单的方法是使用堆栈面板:
<StackPanel Orientation="Horizontal">
<TextBlock Text="Some Text"/>
<CheckBox />
</StackPanel>
回答by Arushi Agrawal
You can edit the checkbox template and over there can place the textblock ahead of the rectangle
您可以编辑复选框模板,在那里可以将文本块放在矩形前面

