TextBlock

TextBlock renders read-only text with optional wrapping, alignment, and trimming.

TextBlock

Basic usage

new TextBlock("Hello, world!");

Dynamic text (bindings)

TextBlock supports a dynamic text provider via a Func<string>:

var count = new State<int>(0);

new TextBlock(() => $"Count: {count.Value}");

Wrapping and trimming

new TextBlock("This is a long line that can wrap.")
    .Wrap(true);

new TextBlock("This is a long single line that will be trimmed.")
    .Wrap(false)
    .Trimming(TextTrimming.EndEllipsis);

Alignment

TextAlignment controls how the text is aligned inside the available width:

new TextBlock("Centered")
    .TextAlignment(TextAlignment.Center)
    .HorizontalAlignment(Align.Stretch);

Defaults

  • Default alignment: HorizontalAlignment = Align.Start, VerticalAlignment = Align.Start

Styling

Use TextBlockStyle to override colors and decorations for a subtree (or for a single TextBlock):

new TextBlock("Accent")
    .Style(TextBlockStyle.Default with
    {
        Foreground = Colors.DeepSkyBlue,
        TextStyle = TextStyle.Bold,
    });

To apply a background only behind the glyphs, set Background:

new TextBlock("Highlighted")
    .Style(TextBlockStyle.Default with
    {
        Background = Colors.Blue,
        Foreground = Colors.White,
    });

To fill the whole bounds with a background, enable FillBackground:

new TextBlock("Banner")
    .HorizontalAlignment(Align.Stretch)
    .Style(TextBlockStyle.Default with
    {
        Background = Colors.Blue,
        Foreground = Colors.White,
        FillBackground = true,
    });