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.
如何在RichTextBox中对文本进行对齐?似乎控制并不直接支持它。所以我在寻找模仿它的方法。理想情况下,我应该将控件的边界固定,文本的结尾与底部对齐。
1 个解决方案
#1
0
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.
文本来自名为PART_ContentHost的ScrollViewer,它位于由RichTextBox包装的TextBoxBase的默认控件模板中。您应该重写控件模板,或者让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.
下面,我做了后者。这是默认控制模板的修改版本,从Blend中提取。我所做的唯一更改是向ScrollViewer添加垂直对齐=“{TemplateBinding垂直对齐}”。
(Also note that it references Microsoft_Windows_Themes which is defined as xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
(还要注意,它引用了microsoft_windows_theme,定义为xmlns: microsoft_windows_theme ="clr-namespace: microsoft.windows . theme;assembly=PresentationFramework.Aero")
I am unsure how this will work if Aero is not on the user's machine)
我不确定如果Aero不在用户机器上,它将如何工作)
<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" />
#1
0
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.
文本来自名为PART_ContentHost的ScrollViewer,它位于由RichTextBox包装的TextBoxBase的默认控件模板中。您应该重写控件模板,或者让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.
下面,我做了后者。这是默认控制模板的修改版本,从Blend中提取。我所做的唯一更改是向ScrollViewer添加垂直对齐=“{TemplateBinding垂直对齐}”。
(Also note that it references Microsoft_Windows_Themes which is defined as xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
(还要注意,它引用了microsoft_windows_theme,定义为xmlns: microsoft_windows_theme ="clr-namespace: microsoft.windows . theme;assembly=PresentationFramework.Aero")
I am unsure how this will work if Aero is not on the user's machine)
我不确定如果Aero不在用户机器上,它将如何工作)
<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" />