Kriss_Kross
Kriss_Kross

Reputation: 569

How to get the correct text Alignment to the Right in AvalonEdit

Initial data - Text alignment to the Left

enter image description here

Creating an event handler enter image description here

and I get the following result, but it's not correct.

enter image description here

The correct behavior is shown in the picture, but this is a TextBox.

enter image description here

How to get the correct text Alignment to the Right in AvalonEdit?

Upvotes: 0

Views: 79

Answers (1)

niKaragua
niKaragua

Reputation: 11

As far as I have learned, AvalonEdit does not have open support for the textAlignment property, such as HorizontalAlignment for the text itself.

Link to the documentation
The official AvalonEdit repository

if you really need to align to the right, then you can do this:

private void TextAligmentRight_Click(object sender, RoutedEventArgs e)
{
    MySuperTextEditor.TextArea.TextView.HorizontalAlignment = HorizontalAlignment.Right;
    TextAligmentRight();
}

private void TextAligmentRight()
{
    var document = MySuperTextEditor.Document;

    int maxLengthLine = 0;

    //We go through all the lines of the document to find the maximum line length.
    for (int lineNumber = 1; lineNumber <= document.LineCount; lineNumber++)
    {
        DocumentLine line = document.GetLineByNumber(lineNumber);

        string lineText = document.GetText(line);
        int lengthLineText = lineText.Length;

        if (lengthLineText > maxLengthLine)
        {
            maxLengthLine = lengthLineText;
        }
    }
    var updatedText = new StringBuilder();

    //We go through all the lines of the document and add indents to align to the right edge.
    for (int lineNumber = 1; lineNumber <= document.LineCount; lineNumber++)
    {
        DocumentLine line = document.GetLineByNumber(lineNumber);

        string lineText = document.GetText(line);
        int lengthLineText = lineText.Length;
        int lengthDifference = maxLengthLine - lengthLineText;

        if (lengthDifference > 0)
        {
            lineText = new string(' ', lengthDifference) + lineText;
        }

        updatedText.AppendLine(lineText);
    }
    MySuperTextEditor.Text = updatedText.ToString();
}

but now, to align to the left, you need to do the opposite.

Upvotes: 0

Related Questions