将复选框内容(文本)放在复选框本身左侧的最简单方法是什么?
答案 0 :(得分:77)
最大化“宽松度”和“正确性”的解决方案是使用RightToLeft
内容制作LeftToRight
复选框:
<CheckBox FlowDirection="RightToLeft">
<TextBlock FlowDirection="LeftToRight" Text="CheckBox Content:" />
</CheckBox>
或者如果你想要一种风格:
<Style TargetType="CheckBox">
<Setter Property="FlowDirection" Value="RightToLeft" />
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<ContentControl FlowDirection="LeftToRight" Content="{Binding}" />
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
答案 1 :(得分:25)
在代码中:
System.Windows.Controls.CheckBox checkBox = new System.Windows.Controls.CheckBox();
checkBox.Content = ":CheckBox Enabled";
checkBox.FlowDirection = System.Windows.FlowDirection.RightToLeft;
在XAML中:
<CheckBox FlowDirection="RightToLeft" Content=":CheckBox Enabled" />
修改强>
用户 punker76 让我注意到冒号&#34;:&#34;必须是要正确显示的文本 infront ,最后(&#34; CheckBox Enabled:&#34;),可能是由于影响流方向对文本元素造成的。好的捕获。
答案 2 :(得分:16)
另一种方法是制作新的自定义样式
<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>
用法:
<CheckBox Style="{StaticResource ContentLeftCheckBoxStyle}" Content="CheckBox:" />
希望有所帮助!
答案 3 :(得分:14)
另一种解决方法是,在定义全新的CheckBox样式时避免上面讨论的流向和大量代码的细微问题,只需使用包含Label(带有所需CheckBox内容字符串)和CheckBox的WrapPanel。 (没有内容字符串)。
<WrapPanel>
<Label Content="Checkbox content"/>
<CheckBox VerticalAlignment="Center" Margin="5,0,0,0"/>
</WrapPanel>
答案 4 :(得分:12)
我花了两个小时,但我找到了最好的决定
<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>
答案 5 :(得分:8)
我知道已经有一段时间了,我已经迟到了。但经过几个复杂的答案并在互联网上搜索了很多,我终于找到了最简单的方法来实现这一点,而不会扭曲here的勾号。
<CheckBox Content="Checked" FlowDirection="RightToLeft">
<CheckBox.Resources>
<Style TargetType="{x:Type Path}">
<Setter Property="FlowDirection" Value="LeftToRight" />
</Style>
</CheckBox.Resources>
</CheckBox>
结果:
答案 6 :(得分:4)
最简单的方法是使用堆栈面板:
<StackPanel Orientation="Horizontal">
<TextBlock Text="Some Text"/>
<CheckBox />
</StackPanel>
答案 7 :(得分:0)
您可以编辑复选框模板,然后可以将文本块放在矩形的前面