Telerik blogs

C# Expressions mean you can now skip the Converters and add logic directly in your XAML using C#. This is great news for .MET MAUI developers!

Improving and optimizing your code should be your top priority every single day. As a .NET MAUI developer, I know Converters are very familiar to you. They’ve helped us transform data and adapt it to the UI. However, we can all agree that sometimes they made our code a bit more verbose than necessary.

But here’s some BIG news 👀 … and honestly, one of the most impactful changes in recent times. The Microsoft team, always thinking about developer experience, now allows us to say goodbye to Converters and start using C# Expressions instead! 🚀

What does this mean? You can now add logic directly in your XAML using C#: perform math operations, use string interpolation, boolean logic and much more. In this article, we’ll walk through before-and-after scenarios so you can clearly see how much code you can save, not to mention how much more readable and team-friendly your code will become.

Grab your coffee ☕ and let’s get started!

Let’s Start!

Today, we’ll dive directly into the code to explore the differences and how we can use them moving forward. Each example will be broken down into subtopics so you can clearly understand the before and after. 🚀

Case 1: Simple Property Binding

When we need to connect the value of a property with the UI, we typically use Bindings followed by the property name. For example, if we want to display values from properties like FullName or Company, we would write something like this:

<Label Text="{Binding FullName}" />

<Label Text="{Binding Company}" />

Applying C# Expressions

Now, what if I told you that you can achieve the same result without using Bindings? 👀

With C# Expressions, you can do it in two different ways:

🔹 Implicit syntax

<Label Text="{FullName}" />

<Label Text="{Company}" />

🔹 Explicit syntax

You can also use the = prefix to make it more explicit that you’re using a C# expression.

<Label Text="{= FullName}" />

<Label Text="{= Company}" />

There is no functional difference between the implicit and explicit approaches; both achieve the same result. You can use whichever style you feel more comfortable with or that best fits your team’s coding standards.

Case 2: String Interpolation

We’re used to formatting strings using StringFormat. For example, if we want to display a price like “Total: $28 USD,” we first need to define the Binding with the property, and then apply the format using StringFormat='Total: ${0} USD'.

If we need to include more properties in the format, we must keep adding placeholders, which can make the expression more complex and harder to read as it grows.

<Label Text="{Binding Price, StringFormat='Total: ${0} USD'}" FontSize="18" TextColor="Green" />

Applying C# Expressions

With C# Expressions, you can do it directly. 😎 Notice how much simpler and more readable the same expression becomes compared to the previous approach:

<Label Text="{=$'Total: ${Price} USD'}" FontSize="18" TextColor="Green" />

Case 3: Multi-root Expressions (Calculations)

I’m speechless 🤯🤯🤯 … you can now perform calculations directly in XAML.

Before seeing how this works now, let’s look at how we’ve traditionally handled this scenario. To achieve this before, we usually needed to create a computed property in the ViewModel, something like:

public decimal Subtotal => UnitPrice * ItemsCount;

And then bind it in XAML:

<Label Text="{Binding Subtotal, StringFormat='Subtotal: ${0:F2}'}" 
    FontSize="20" 
    FontAttributes="Bold" />

Now with C# Expressions

One of the most exciting improvements is that we can now perform calculations directly in XAML.

For example, if we want to display the subtotal of a purchase based on the unit price and the number of items, we no longer need a computed property or a converter for that. We can write the expression exactly where we need it:

<Label Text="{=$'Subtotal: ${UnitPrice * ItemsCount:F2}'}" 
    FontSize="20" 
    FontAttributes="Bold" />

Case 4: Boolean Negations

In case you still had any doubts about saying goodbye to Converters … 😆

Before, inverting a boolean value was not that straightforward. If you wanted to show or hide an element based on the opposite value of a property, you couldn’t do it directly in XAML. This required creating a converter, registering it in the resources, and then applying it in the binding. A process that, for such a small piece of logic, ended up being unnecessarily complex.

For example, let’s look at this scenario where we want to display a message when the user is not logged in:

<Label Text="Please log in to continue" 
    IsVisible="{Binding IsLoggedIn, Converter={StaticResource BooleanInvertConverter}}" 
    TextColor="Red" 
    FontSize="16" />

And also:

<local:BooleanInvertConverter x:Key="BooleanInvertConverter" />

public class BooleanInvertConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)C
=> !(bool)value;

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
=> !(bool)value; 
}

Now with C# Expressions

Look how simple this becomes with just this small piece of code:

<Label Text="Please log in to continue" 
    IsVisible="{=!IsLoggedIn}" 
    TextColor="Red" 
    FontSize="16" />

Just one line. No converters, no extra classes, no additional setup. 🤩

And of course, you can also work with boolean expressions without negation, like this:

<Switch IsToggled="{HasAccount}" />

Case 5: Lambda Event Handlers

Traditionally, to handle events in .NET MAUI, we needed to create methods in the code-behind or define Commands in the ViewModel. This meant more code, more structure and, in many cases, more complexity than necessary.

But before we see how this looks now, let’s take a look at how we’ve been doing it so far:

<Button Text="{Binding ClickCount, StringFormat='Clicked {0} times'}" 
    Command="{Binding IncrementCommand}" 
    BackgroundColor="DarkOrange" 
    TextColor="White" />

In the view model:

public ICommand IncrementCommand { get; } 
public int ClickCount { get; set; }
 
public MainViewModel()
{ 
	IncrementCommand = new Command(() =>
{ 
    ClickCount++; 
}); 
}

Now with C# Expressions

Thanks to C# Expressions, we can also use lambda expressions directly in XAML to handle events. 🚀 This allows us to execute logic inline, exactly where we need it, making the code more direct, cleaner and much easier to read for simple interactions.

<Button Text="{=$'Clicked {ClickCount} times'}" 
    Clicked="{(s, e) => ClickCount++}" 
    BackgroundColor="DarkOrange" 
    TextColor="White" />

Conclusion

And that’s it! 🎉 In this article, we explored how C# Expressions are changing the way we build .NET MAUI apps. From simple bindings, string formatting and calculations, to boolean logic and even event handling… we saw how we can now write less code, make it more direct, and achieve things that previously required much more setup and effort.

I hope this article helps you start simplifying your own code and encourages you to take advantage of this more modern and expressive approach in your .NET MAUI applications.

If you have any questions or would like me to dive deeper into any of these scenarios, feel free to leave a comment, I’ll be happy to help you! 💚

See you in the next article! 🙋‍♀️🚀

Reference

I used a video by Gerald Versluis as a reference for creating this article:


LeomarisReyes
About the Author

Leomaris Reyes

Leomaris Reyes is a software engineer from the Dominican Republic specializing in mobile development. She is a 7-time Microsoft MVP and actively builds mobile applications while sharing practical knowledge through technical articles and developer-focused content.

Through her work, she explains programming concepts, developer tools and real-world development practices to help developers grow in their careers.

You can follow her on Instagram and TikTok at @leomarisreyes.dev, read her articles on AskXammy, and connect with her on LinkedIn, where she shares tutorials, insights and resources for developers.

Related Posts

Comments

Comments are disabled in preview mode.