2

如何在RichTextBox中对齐文本?看来控制不直接支持它。所以我正在寻找方法来模拟它。理想情况下,我会将控件的边界固定,文本的末尾与底部对齐。如何在WPF RichTextBox中对齐文本

回答

0

该文本来自名为PART_ContentHost的ScrollViewer,该文本位于由RichTextBox包装的TextBoxBase的默认控件模板内。您应该重写控件模板,并让ScrollViewer将VerticalAlignment声明为Bottom,或者将模板绑定到VerticalContentAlignment。

下面,我做了后者。这是从Blend中提取的默认控制模板的修改版本。我所做的唯一更改是向ScrollViewer添加VerticalAlignment =“{TemplateBinding VerticalAlignment}”

(另请注意,它引用其定义为的xmlns Microsoft_Windows_Themes:Microsoft_Windows_Themes =“CLR的命名空间:Microsoft.Windows.Themes;装配= PresentationFramework.Aero”

我不确定如何,如果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" /> 
+0

我试图改变与WPF Inspector中的财产,这是行不通的!你试过了吗? – Alfa07 2011-06-02 09:37:11

+0

对不起,我在Style的BasedOn属性中有一个拼写错误。现在就试试? – Adrian 2011-06-02 15:43:52