Skip to content

[Feature] Extension to remove whitespace between TextBlock Runs #3370

@MartinZikmund

Description

@MartinZikmund

Describe the problem this feature would solve

While developing apps for high-school physics students, I noticed that writing TextBlock content with multiple Runs with proper whitespace is a bit hard:

For example to output "(1,6×10^19)" I have to do:

<TextBlock>
    <Run Text="(1,6×10" /><Run Text="&#8315;19" Typography.Variants="Superscript" /><Run Text=")" />
</TextBlock>

And this all has to be on a single line of XAML because whitespace between Runs generates a space in the output. This may result in long, hard to read lines of code for multiple consecutive Runs.

Describe the solution

A solution similar to what is described in this blogpost with a small adjustment. Attached property TrimWhitespaceRuns on TextBlock would go through Runs and for all which havestring.IsNullOrWhitespace(Text) it would set their Text to string.Empty. This way the runs stay there (in case someone the developer wanted to set them to something else later). It would also need to skip Runs which have a Binding set for the Text property. A second attached property would allow preserving whitespace Run.

API

  • TextBox.TrimWhitespaceRunsProperty
  • Run.PreserveWhitespaceProperty

The above example would become:

<TextBlock toolkit:TextBlockExtensions.TrimWhitespaceRuns="true">
    <Run Text="(1,6×10" />
    <Run Text="&#8315;19" Typography.Variants="Superscript" />
    <Run Text=")" />
</TextBlock>

Describe alternatives you've considered

Additional context & Screenshots

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions