如何在 WPF RichTextBox 中底部对齐文本 [英] How to bottom align text in WPF RichTextBox

查看:105
本文介绍了如何在 WPF RichTextBox 中底部对齐文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 RichTextBox 中底部对齐文本?似乎控件不直接支持它.所以我正在寻找模仿它的方法.理想情况下,我会将控件的边界固定并且文本的结尾与底部对齐.

How I can bottom align text in RichTextBox? It seems that control doesn't support it directly. So I am looking for ways to emulate it. Ideally I would have the boundary of the control fixed and end of text aligned with the bottom.

推荐答案

文本来自 TextBoxBase 的默认控件模板中名为 PART_ContentHost 的 ScrollViewer,该模板由 RichTextBox 包装.您应该覆盖控件模板,并让 ScrollViewer 将其 VerticalAlignment 声明为 Bottom,或者将其模板绑定到 VerticalContentAlignment.

The text comes from a ScrollViewer named PART_ContentHost inside the default control template for the TextBoxBase which is wrapped by the RichTextBox. You should override the control template and either have the ScrollViewer declare its VerticalAlignment to be Bottom, or have it template bind to the VerticalContentAlignment.

下面,我做了后者.这是从 Blend 中提取的默认控件模板的修改版本.我所做的唯一更改是将 VerticalAlignment="{TemplateBinding VerticalAlignment}" 添加到 ScrollViewer.

Below, I've done the latter. This is a modified version of the default control template as pulled from Blend. The only change I've made is to add VerticalAlignment="{TemplateBinding VerticalAlignment}" to the ScrollViewer.

(另请注意,它引用了 Microsoft_Windows_Themes,定义为 xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"

(Also note that it references Microsoft_Windows_Themes which is defined as xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"

如果 Aero 不在用户的机器上,我不确定这将如何工作)

I am unsure how this will work if Aero is not on the user's machine)

<Style x:Key="BottomAlignedTextBoxBaseStyle" 
       TargetType="TextBoxBase"
       BasedOn="{StaticResource {x:Type TextBoxBase}}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBoxBase}">
                <Microsoft_Windows_Themes:ListBoxChrome x:Name="Bd"
                                                        BorderBrush="{TemplateBinding BorderBrush}"
                                                        BorderThickness="{TemplateBinding BorderThickness}"
                                                        Background="{TemplateBinding Background}"
                                                        RenderMouseOver="{TemplateBinding IsMouseOver}"
                                                        RenderFocused="{TemplateBinding IsKeyboardFocusWithin}"                                                       SnapsToDevicePixels="true">
                    <ScrollViewer x:Name="PART_ContentHost"
                                  SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
                                  VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                </Microsoft_Windows_Themes:ListBoxChrome>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled"
                             Value="false">
                        <Setter Property="Background"
                                TargetName="Bd"
                                Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                        <Setter Property="Foreground"
                                Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

然后,要使用它,只需说:

Then, to use it, simply say:

<RichTextBox Style="{StaticResource BottomAlignedTextBoxBaseStyle}" 
             VerticalContentAlignment="Bottom" />

这篇关于如何在 WPF RichTextBox 中底部对齐文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆