Skip to content

Scrolling and Virtualization

Pavel edited this page Oct 19, 2026 · 6 revisions

English | Русский

MarkdownViewer has no scroll bars of its own. It lives inside the scrolling you already have and creates controls only for the blocks that are visible through every scrolling container around it. The other blocks exist as light models: selection, copying, search and the table of contents work with the whole document anyway.

In a ScrollViewer

The usual case: one document fills a pane.

A window or a user control:

<ScrollViewer>
    <md:MarkdownViewer Markdown="{Binding Text}" Margin="24,16" />
</ScrollViewer>

As a part of a page

The viewer can stand on a page next to other content, with one scroll bar for everything. Only the visible part of the document is created, even when the viewer is much taller than the window.

A window or a user control:

<ScrollViewer>
    <StackPanel Margin="24,16">
        <TextBlock Text="{Binding Title}" FontSize="24" />
        <md:MarkdownViewer Markdown="{Binding Body}" />
        <Button Content="Reply" HorizontalAlignment="Left" />
    </StackPanel>
</ScrollViewer>

In a virtualized list

A chat or a feed: a viewer per message inside a virtualized list. The list creates only visible messages, and a long message creates only its visible blocks.

A window or a user control:

<ItemsControl ItemsSource="{Binding Messages}"
              VirtualizingPanel.IsVirtualizing="True"
              VirtualizingPanel.ScrollUnit="Pixel"
              ScrollViewer.CanContentScroll="True">
    <ItemsControl.Template>
        <ControlTemplate TargetType="ItemsControl">
            <ScrollViewer CanContentScroll="True">
                <ItemsPresenter />
            </ScrollViewer>
        </ControlTemplate>
    </ItemsControl.Template>
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <md:MarkdownViewer DocumentKey="{Binding Id}" Markdown="{Binding Text}" Margin="8" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Keep VirtualizingPanel.ScrollUnit="Pixel": scrolling to a source line, a range or an anchor moves the scroll position by pixels.

Give each message its DocumentKey. With VirtualizingPanel.VirtualizationMode="Recycling" the list hands the viewer of a message that scrolled away to another message, and without a key the selection and the widget state of the old message would move into the new one — see Live Preview.

Sticking to the edge

When new content comes at the end — an answer of a model, a feed of messages, a log — it is handy to keep the scrolling at the edge. StickyScroll.Edge on a ScrollViewer of your application sticks it to an edge: Bottom to the bottom, Top to the top when new content comes at the start. While the scrolling is stuck, it returns to the edge however the content changes.

The scrolling comes unstuck when it is moved away from the edge — you can read older content, and new chunks do not pull you down — and sticks again when it is brought back to the edge. With StickyScroll.CanUnstick="False" it stays at the edge always. StickyScroll.IsStuck — read-only — tells whether it is stuck now: for example, to show a «to the latest» button.

A window or a user control (xmlns:md="https://github.com/porohkun/Beresta.Markdown.Wpf" on the root element):

<ScrollViewer md:StickyScroll.Edge="Bottom" md:StickyScroll.CanUnstick="True">
    <md:MarkdownViewer Markdown="{Binding Text}" Margin="24,16" />
</ScrollViewer>

In a virtualized list the same properties go on the ScrollViewer from the template of the list.

Without scrolling

Without a scrolling container around it, the viewer creates all blocks and takes the height of the whole document. That is fine for short texts: a tooltip, a card, a dialog.

Nested scrolling

Wide tables and code blocks scroll sideways on their own but pass the mouse wheel to the page, so the document keeps scrolling while the pointer is over them; Shift + wheel scrolls them sideways. A widget of yours with its own ScrollViewer gets the same behavior with one attached property.

Your widget template (see Extensions):

<ScrollViewer HorizontalScrollBarVisibility="Auto"
              VerticalScrollBarVisibility="Disabled"
              md:NestedScroll.PassWheelToParent="True">
    <TextBlock Text="{Binding Wide}" />
</ScrollViewer>

New text keeps the view in place

A new Markdown value is applied as edits: unchanged blocks keep their controls and measured heights, and the visible part does not jump when the text changes above it — see Live Preview.

Beresta.Markdown.Wpf

Basics

Working with Text

Appearance

Integration

Editing

Your Own Syntax

Ready-made Extensions


По-русски

Основы

Работа с текстом

Оформление

Встраивание в приложение

Правка

Свой синтаксис

Готовые расширения

Clone this wiki locally