我正在使用我的复选框模板:
<Style x:Key="StyleCheckboxError" TargetType="{x:Type CheckBox}">
<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="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type CheckBox}">
<Grid x:Name="IGridMain">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CheckStates">
<VisualState x:Name="Checked">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="IRectError">
<DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Unchecked"/>
<VisualState x:Name="Indeterminate"/>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Rectangle x:Name="IRectError" Fill="{DynamicResource BrushTBPRed}" Margin="2" Stroke="Black" Visibility="Hidden"/>
<Border BorderBrush="{DynamicResource BrushTBPShadow}" BorderThickness="0,0,2,2" Margin="0" Padding="0" Height="Auto" VerticalAlignment="Stretch"/>
<Border BorderBrush="{DynamicResource BrushTBPBlack}" BorderThickness="2,2,0,0" Margin="0" Padding="0" Height="Auto" VerticalAlignment="Stretch"/>
<ContentPresenter Margin="2,2,0,0" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="HasContent" Value="true">
<Setter Property="FocusVisualStyle" Value="{StaticResource CheckRadioFocusVisual}"/>
<Setter Property="Padding" Value="4,0,0,0"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
如您所见,ContentPresenter是居中对齐的。对于短内容,一切都很好但是当内容是多行的时,左对齐文本位于复选框的中心。如何更改ContentPresenter中文本的TextAlignment
谢谢..
答案 0 :(得分:1)
答案 1 :(得分:1)
我自己的问题的解决方案是:
你已经完成了!
答案 2 :(得分:0)
您可以使用ContentPresenter的ContentTemplate属性。 首先在参考资料中定义DataTemplate
<DataTemplate x:Key="TextContentTemplate">
<TextBlock Text="{Binding}" TextAlignment="Left" TextWrapping="Wrap"/>
</DataTemplate>
然后在ContentPresenter中使用它:
<ContentPresenter Margin="2,2,0,0" HorizontalAlignment="Center" VerticalAlignment="Center" ContentTemplate="{StaticResource TextContentTemplate}"/>
我希望它有所帮助