WPF in .NET 9.0: Hyphen-based Ligatures

In the last blog posts about WPF in .NET 9.0, you learned about Windows 11 Theming and about the Accent Color. In this blog post, let’s look at a smaller feature called hyphen-based ligatures.

What are Ligatures?

Ligatures are special character cominations used to create a single symbol. For example the characters “->” could be used to create an arrow symbol like this: ➡.

Which ligatures are supported depends on the font that you’re using. Microsoft’s Cascadia Code font supports a lot of ligatures.

If you’ve never heard about Cascadia Code: It’s a monospaced font. Monospaced means that all characters have the same width, which is great for programming. Cascadia Code comes as a system font pre-installed with Windows 11. It’s used for example by the Windows Terminal.

The ligatures supported by Cascadia Code can be found in the GitHub repo of the font. They look like below.

As you can see, there are some ligatures that use hyphens. These are supported by WPF’s TextBlock since .NET 9.0. Let’s take a look at an example.

Hyphen-based Ligatures in WPF

In WPF, the ligatures with hyphens were not supported before .NET 9.0. To try the feature, let’s create a new WPF project and put this TextBlock into the Grid in the MainWindow.xaml file:

<TextBlock FontFamily="Cascadia Code">
    Hyphen-based ligatures in WPF: --&gt; &lt;--
</TextBlock>

The XML entity references &gt; (greater than) and &lt; (less than) are used to define the characters > and <. So, here the characters --> and <-- are defined.

Before .NET 9.0, the TextBlock has the following output. As you can see, the hyphen-based ligatures are not supported.

Before .NET 9.0, hypen-based ligatures are not supported in WPF.

With .NET 9.0 or later, the same TextBlock looks like below. As you can see, the characters --> and <-- are combined into the arrow symbols of the Cascadia Code font.

With .NET 9.0, hyphen-based ligatures are supported in WPF’s TextBlock.

If you want to play a bit with ligatures, you can also use a TextBox instead of a TextBlock. Just put the following TextBox with the Cascadia Code font into your WPF app and then you can start typing and seeing the results live:

<TextBox FontFamily="Cascadia Code"/>

Let’s run the app in .NET 8.0 and type some text into the TextBox. This time I use these characters: <!--. The output looks like below with .NET 8.0.

Another hyphen-based ligature not supported in .NET 8.0.

In .NET 9.0 or later, the same text in the same TextBox leads to the expected symbol of the Cascadia Code font that you see in the screenshot below.

In .NET 9.0, also this hyphen-based ligature is supported and the expected symbol is shown.

Summary

If you build an application, where a font with ligatures like Cascadia Code is used, then the support for hyphen-based ligatures can be a big deal for you. It’s a simple, but powerful new feature of WPF in .NET 9.0.

Thanks for reading,
Thomas

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.