3

I have a richtextbox in WPF,

<RichTextBox Name="_RichTextBox" />

And in the code-behind i was able to auto-expand the richtextbox height on the text change

mRichTextBox.SelectionChanged += OnSelectionChanged;

async void OnSelectionChanged(object sender, RoutedEventArgs e)
        {
            mRichTextBox.Height = mRichTextBox.Document.LineHeight;
        }

But, the LineHeight is coming NaN for me and so does the Height ,

Is there any other way to achieve this.

Any help is Appreciated!!

6
  • is it a possibility to count the lines and add the font height + a constant to that to calculate the height?
    – Jens
    Mar 2, 2015 at 13:08
  • yea i was thinking of that Mar 2, 2015 at 13:10
  • i have no practice with RichTextBox, but thats the first what came to my mind..
    – Jens
    Mar 2, 2015 at 13:12
  • It's my last shot but i was looking for something descent and elegant Mar 2, 2015 at 13:13
  • 3
    What's wrong with just putting it in a Grid with a RowDefinition Height="Auto"and letting that do the resizing?
    – Sheridan
    Mar 2, 2015 at 13:16

1 Answer 1

5

Auto Resize of the Height (in XAML):

<Grid>
    <RichTextBox Height="Auto" Width="200">
        <FlowDocument>
            <Paragraph>
                <Run Text="RichTextBox"/>
            </Paragraph>
        </FlowDocument>
    </RichTextBox>
</Grid>

the Width is constant. the Height is 1 Line, when the Text isn't bigger than one row.

One Line

One Line

More than one line + Auto Resize

More than one line + Auto Resize

1
  • 6
    Is this possible to do for width? Autoresize width? May 27, 2015 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.