Repository navigation
Coordinates
English | Русский
Your application draws its own things over the document or next to it: buttons in a margin, frames, lines between two panes. The viewer tells where pieces of text and source lines are on the screen — right in the coordinates of your element — and when the layout changes.

GetCoordinates(element) gives MdCoordinates — the geometry of the document in the coordinates of
the element. It is computed at every call from the current layout.
| Member | What it gives |
|---|---|
RangeRects(range), RangeBounds(range)
|
the rectangles of a piece of the text map screen line by screen line and their common frame; only for created blocks |
LineTop(line) |
the top of a source line; a fractional part is a part of the line |
LinesRect(first, last) |
the band of source lines across the width of the viewer: from the top of the first to the top of the line after the last |
LineAt(point) |
the source line under a point; a fractional part is a part of the line |
PositionAt(point, snapToText) |
the position of the text map under a point; with snapToText — at the nearest text, without it outside the text — null
|
VisibleLines() |
the source lines at the top and the bottom edges of the visible area of the scrolling |
VisibleRows() |
the source lines that have their own place in the visible area — where the margin puts their numbers: the top and the height of each |
A line inside a paragraph stands where it starts on the screen: a long source line that wraps into
several screen lines does not shift the next one. The same answers in the viewer's own coordinates
are GetRangeRects, GetRangeBounds, GetSourceLineOffset, GetSourceLineAt and
GetPositionFromPoint — see Selection and
Source Lines and Two Panes.
ViewportChanged comes when the layout of the visible part may have changed: the scrolling moved
outside the viewer or inside a block — a code block or a table scrolled sideways; heights of blocks
became known; a new text came. Draw your things again on it.
A column of your application next to the viewer, in the same scrolling. Buttons are created only for the
visible lines, stand at their lines and move again on ViewportChanged.
MainWindow.xaml:
<ScrollViewer>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="28" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Canvas x:Name="Marks" />
<md:MarkdownViewer x:Name="Viewer"
Grid.Column="1"
Markdown="{Binding Text}"
ViewportChanged="OnViewportChanged" />
</Grid>
</ScrollViewer>MainWindow.xaml.cs, in the window class (using System.Windows.Controls.Primitives;):
private readonly HashSet<int> _bookmarks = [];
private void OnViewportChanged(object? sender, EventArgs e)
{
Marks.Children.Clear();
foreach (var (line, top, height) in Viewer.GetCoordinates(Marks).VisibleRows())
{
var button = new ToggleButton { Width = 12, Height = 12, IsChecked = _bookmarks.Contains(line), Tag = line };
button.Click += OnBookmarkClick;
Canvas.SetLeft(button, 8);
Canvas.SetTop(button, top + (height - button.Height) / 2);
Marks.Children.Add(button);
}
}
private void OnBookmarkClick(object sender, RoutedEventArgs e)
{
var line = (int)((ToggleButton)sender).Tag;
if (!_bookmarks.Remove(line))
{
_bookmarks.Add(line);
}
}The showcase scenario «Margin buttons» shows the bookmarks as marks of its own layer.
Two viewers in their own scrolling and a canvas between them: a changed piece is a figure from the band of its lines on the left to the band on the right. Both bands come from the coordinates of one canvas.

MainWindow.xaml.cs, drawn again on ViewportChanged of both viewers (using System.Windows.Shapes;);
_hunks are the changed pieces of your diff — the first and the last line on each side:
private void DrawConnections()
{
Connections.Children.Clear();
var left = LeftViewer.GetCoordinates(Connections);
var right = RightViewer.GetCoordinates(Connections);
foreach (var (leftFirst, leftLast, rightFirst, rightLast) in _hunks)
{
if (left.LinesRect(leftFirst, leftLast) is { } from && right.LinesRect(rightFirst, rightLast) is { } to)
{
Connections.Children.Add(new Polygon
{
Points = { new(0, from.Top), new(Connections.ActualWidth, to.Top), new(Connections.ActualWidth, to.Bottom), new(0, from.Bottom) },
Fill = (Brush)FindResource("Md.Mark.Changed.Brush")
});
}
}
}A piece that is empty on one side is a point there: LineTop of the line it stands before. The full
example is the showcase scenario «Two panes»; see also two panes.
Beresta.Markdown.Wpf · Issues · MIT License
Basics
- Getting Started
- How It Renders
- Supported Markdown
- Settings and Hooks
- Scrolling and Virtualization
- Comparison
Working with Text
Appearance
Integration
- Application Links
- Images
- Untrusted Text
- Task Lists
- Source Lines and Two Panes
- Coordinates
- Live Preview
Editing
Your Own Syntax
Ready-made Extensions
Основы
- Начало работы
- Как рисует вьюер
- Что поддерживается
- Настройки и перехват
- Прокрутка и виртуализация
- Сравнение
Работа с текстом
Оформление
Встраивание в приложение
- Ссылки приложения
- Картинки
- Чужой текст
- Списки задач
- Строки исходника и две панели
- Координаты
- Живой предпросмотр
Правка
Свой синтаксис
Готовые расширения