RichTextBoxでテキストを下揃えにするにはどうすればよいですか?コントロールはそれを直接サポートしていないようです。だから私はそれをエミュレートする方法を探しています。理想的には、コントロールの境界を固定し、テキストの終わりを下に揃えます。
1 に答える
テキストは、RichTextBox によってラップされる TextBoxBase のデフォルト コントロール テンプレート内の PART_ContentHost という名前の ScrollViewer から取得されます。コントロール テンプレートをオーバーライドし、ScrollViewer でその VerticalAlignment を Bottom として宣言するか、VerticalContentAlignment にテンプレート バインドする必要があります。
以下では、後者を行いました。これは、Blend から取得した既定のコントロール テンプレートの修正バージョンです。私が行った唯一の変更は、VerticalAlignment="{TemplateBinding VerticalAlignment}"を ScrollViewer に追加することです。
(また、xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" として定義されている Microsoft_Windows_Themes を参照していることにも注意してください。
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>
次に、それを使用するには、単に次のように言います。
<RichTextBox Style="{StaticResource BottomAlignedTextBoxBaseStyle}"
VerticalContentAlignment="Bottom" />