Ilya  Lapan Ilya Lapan - 8 days ago 5
C# Question

WPF: ScrollViewer total content height

I am working on a WPF app. Here is my interface:

<ScrollViewer x:Name="ScrollView" CanContentScroll="True">
<RichTextBox Padding="10" x:Name="TextArea" Background="Transparent" Opacity="0.95" />
</ScrollViewer>


I want to load a text file in to the RichTextBox, which is bigger then the size of the screen and then find out the total size of the contents of scroll viewer(i.e. how far can I actually scroll). In the documentation it says:


ScrollViewer.ExtentHeight Property

A Double that represents the vertical size of the extent. The default
is 0.0.

If CanContentScroll is true, the values of the ExtentHeight,
ScrollableHeight, ViewportHeight, and VerticalOffset properties are
number of items. If CanContentScroll is false, the values of these
properties are Device Independent Pixels.


If understand correctly, the documentation is saying that the
ExtentHeight
should give me the height of my content in units of contents sizes, if
CanContentScroll
is
true
. My RichTextBox is the content of scroll view. So if my RichTextBox is 1000 pixels long, for example, and 500 pixels are visible on screen, then
ExtentHeight
should be 2. However, when I load up an empty
RichEditBox
which completely fits inside
ScrollViewer
without scrolling, and has
ActualHeight
equal to 567,
ExtentHeight
is equal to 37.96, which makes no sense to me. I expect if to be equal to 1 or less then 1. So how do I find the total content size of the TextBox inside ScrollViewer.

Answer

I think the cause of my issue was that I had my RichTextBox in ScrollViewer. RichTextBox has it own scrolling capabilities, and by sticking it in ScrollViwer it got all mixed up. So I got rid of ScrollViewver and used RichTextBox only with its HeightExtent property.

Comments