开发者

WPF Multiline Text Aligniment in ContentPresenter

开发者 https://www.devze.com 2023-02-04 04:08 出处:网络
I\'m using a template for my checkbox : <Style x:Key=\"StyleCheckboxError\" TargetType=\"{x:Type CheckBox}\">

I'm using a template for my checkbox :

<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>开发者_StackOverflow社区
                                    <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>

As you see ContentPresenter is center-aligned. Everything is fine for short contents but when the content is multi-lined, Left-aligned Text sits in the center of the checkbox.. How can I change the TextAlignment of a text in a ContentPresenter

Thanks..


The solution for my own question is this :

  • Add a TextBlock to your template
  • Use Template Binding > Content for its Text property (And other properties if needed)
  • Align the TextBlock (Strech for Horizontal and Center for Vertical)
  • Use Center for TextBlock's Text Alignment property under Paragraph tab.
  • Remove Content Presnter

You're done!


You might want to take a look at the Control.HorizontalContentAlignment property.


You could use the ContentTemplate property of the ContentPresenter. First define your DataTemplate in the Resources

<DataTemplate x:Key="TextContentTemplate">
    <TextBlock Text="{Binding}" TextAlignment="Left" TextWrapping="Wrap"/>
</DataTemplate>

And then use it in your ContentPresenter:

<ContentPresenter Margin="2,2,0,0" HorizontalAlignment="Center" VerticalAlignment="Center" ContentTemplate="{StaticResource TextContentTemplate}"/>

I hope it helps

0

精彩评论

暂无评论...
验证码 换一张
取 消