Dilmah
Dilmah

Reputation: 1127

Change the alignment of Rich text Box in WPF

How to change the vertical and horizontal alignment of RichText box. i have tried all the api's horizontalalignment and vertical alignment. but nothing works.

Upvotes: 4

Views: 4283

Answers (3)

Sadra M.
Sadra M.

Reputation: 1542

What you are looking for are called EditingCommands

https://learn.microsoft.com/en-us/dotnet/api/system.windows.documents.editingcommands?view=netframework-4.8

EditingCommands.AlignLeft.Execute(null, richTextBox);
//or
EditingCommands.AlignRight.Execute(null, richTextBox);     

You can find all kinds of text editing commands here.

Upvotes: 0

Yun CHEN
Yun CHEN

Reputation: 6648

For horizontal alignment of RichTextBox, you could change it by FlowDocument's property TextAlignment.

Example:

<RichTextBox x:Name="richTextBox"  Height="30"  >
     <FlowDocument TextAlignment="Center">
          <Paragraph>
               <Run Text="RichTextBox"/>
           </Paragraph>
     </FlowDocument>
</RichTextBox>

Upvotes: 4

Hussaini Said Adam
Hussaini Said Adam

Reputation: 33

This property returns the current alignment. Setting this property adjusts the contents of the RichTextBox to reflect the specified alignment.

<!--Create a RichTextBox and a Button.-->
   <StackPanel>
   <RichTextBox x:Name="MyRTB" Height="100" Width="400" />
   <Button x:Name="MyButton1" Content="Right-Align" Height="30" Width="100" Click="button_Click" />
</StackPanel>

Right-align the content in RichTextBox on clicking the button.

private void button_Click(object sender, RoutedEventArgs e)
{
    BlockCollection MyBC = MyRTB.Blocks;
    foreach (Block b in MyBC)
        b.TextAlignment = TextAlignment.Right;               
}

Upvotes: 3

Related Questions