Skip to content
Bruno Sonnino
Menu
  • Home
  • About
Menu

Modernizing your WPF application with the Fluent theme

Posted on 6 July 2024

You have an old WPF application that you would like to modernize, making its UI more up-to-date and in-line with Windows 11 theming.

There are some ways to do it, and the first thing that comes to mind is to create a Resource Dictionary with the theme and apply it to your app.

This is a challenging task, as you would have to create resources for all controls, including the light and dark themes. It's not a task for everyone.

Another option is to use an available theme on the internet, such as the Material Design in in XAML Toolkit or MahApps. This is much better than designing your own theme but has a drawback: Windows themes are constantly evolving, and some adjustments may be needed as the design changes.

.NET 9.0 introduces something new to WPF: the Fluent theme. Note that .NET 9 is still in preview, so things may change. .NET 9 is expected to be released in November 2024, and the available bits may differ from the final release. Additionally, the current version of Visual Studio doesn't support .NET 9, so you'll need to use the preview version, which you can obtain here

In this article, I will show you how to upgrade your current application to .NET 9 and apply the Fluent theme to achieve a modernized app. I will use the WPF Calculator Demo, that is part of the WPF Samples, available here.

After compiling and running it, you will see something like this:

While the calculator's functionality is fine, its UI is somewhat dated and should be upgraded to Windows 11. The first step is to install .NET 9 from here if you haven't done so.

Next, we will use the Upgrade Assistant to update the app to .NET 9. Some time ago, I wrote this article demonstrating how to use the CLI tool to upgrade your app to .NET 6. This method still works and allows you to upgrade to .NET 9. For now, I will use Visual Studio to upgrade the app.

The first step is, in Visual Studio, install the Upgrade Assistant Extension. In Extensions/Manage Extensions, search for Upgrade Assistant:

When you do that, you will get a new menu option when you right-click the project file: Upgrade:

When you select it, it will start to upgrade your app to .NET 9. You will choose the InPlace Upgrade and then you will be able to select the target framework:

By clicking Next and Upgrade, Visual studio will ask you to save the .sln file and will upgrade your app to .NET 9. You can run the application and will see that nothing has changed.

With the upgrade complete, we can add the theme. To do that, just go to App.xaml and add these lines

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary
                    Source="pack://application:,,,/PresentationFramework.Fluent;component/Resources/Fluent.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>
XML

When you run the application again, you will get something like this:

Although the app has the Fluent Design, it has many UI issues, that have to be fixed:

  • The menu isn't shown
  • The buttons aren't square
  • The Memory text is clipped at the bottom and the right
  • The text in the result box isn't vertically aligned

We must fix these issues, to have a nice calculator. You can let the app running and make the changes in the code, they will reflect immediately in the UI. I really think that's a life saver.

Let's start with the menu not showing. In the menu declaration in MainWindow.xaml, we have:

<Menu DockPanel.Dock="Top" Height="26">
XML

If we set the height to 45, the menu shows fine.

For the vertical alignment of the text in the TextBox, we just have to add the VerticalContentAlignment property and set it to Center:

<local:MyTextBox Grid.ColumnSpan="9" x:Name="DisplayBox" Height="30" Margin="5" Padding="5" VerticalContentAlignment="Center"/>
XML

The Memory text has two problems: the ColumnSpan is too small and there is a vertical top margin that pushes the text too low. We will fix both with this code:

<TextBlock Name="BMemBox" Grid.Column="3" Grid.Row="1" Margin="10" Grid.ColumnSpan="5">Memory: [empty]</TextBlock>
XML

For the buttons, we could change the style one by one, but I prefer to add a style to all buttons at once, adding a style in the Window Resources:

<Window.Resources>
    <Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">
        <Setter Property="HorizontalAlignment" Value="Stretch" />
        <Setter Property="VerticalAlignment" Value="Stretch" />
        <Setter Property="Margin" Value="2" />
    </Style>
</Window.Resources>
XML

We are setting the HorizontalAlignment and VerticalAlignment to Stretch and giving a margin of 2 pixels to them.

That's it! We've successfully modernized our WPF application, with buttons with rounded corners and a more modern UI:

As a bonus, our app changes with the Windows selected theme:

.NET 9 is bring a very welcome addition, the new Fluent theme. To add it, it's just a matter of adding a couple of lines to your App.xaml. There may be a couple of UI fixed to be done, but I think that that's a small price to have your current app modernized.

All the source code for this app is at https://github.com/bsonnino/ModernizeWPF

6 thoughts on “Modernizing your WPF application with the Fluent theme”

  1. Pingback: Dew Drop – July 8, 2024 (#4222) – Morning Dew by Alvin Ashcraft
  2. Muxammadamin says:
    24 August 2024 at 13:43

    Good day!
    In my case Visual Studio cannot locate the resource : “Resources/Fluent.xaml”.
    How can i fix this? I would appreciate your reply, thanks in advance.

    Reply
    1. sonnino@gmail.com says:
      24 August 2024 at 17:50

      Are you using .NET 9 preview? It’s only available in .NET 9

      Reply
      1. Muxammadamin says:
        25 August 2024 at 07:42

        yes, im using .net9 preview

        Reply
        1. Muxammadamin says:
          25 August 2024 at 08:09

          Source=”pack://application:,,,/PresentationFramework.Fluent;component/Themes/Fluent.xaml”

          Reply
  3. Muxammadamin says:
    25 August 2024 at 07:56

    ah found solution, seems the paths is actually changed :

    thank you

    Reply

Leave a Reply Cancel reply

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

  • May 2025
  • December 2024
  • October 2024
  • August 2024
  • July 2024
  • June 2024
  • November 2023
  • October 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • April 2020
  • March 2020
  • January 2020
  • November 2019
  • September 2019
  • August 2019
  • July 2019
  • June 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • June 2017
  • May 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • March 2016
  • February 2016
  • October 2015
  • August 2013
  • May 2013
  • February 2012
  • January 2012
  • April 2011
  • March 2011
  • December 2010
  • November 2009
  • June 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • July 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • Development
  • English
  • Português
  • Uncategorized
  • Windows

.NET AI Algorithms asp.NET Backup C# Debugging Delphi Dependency Injection Desktop Bridge Desktop icons Entity Framework JSON Linq Mef Minimal API MVVM NTFS Open Source OpenXML OzCode PowerShell Sensors Silverlight Source Code Generators sql server Surface Dial Testing Tools TypeScript UI Unit Testing UWP Visual Studio VS Code WCF WebView2 WinAppSDK Windows Windows 10 Windows Forms Windows Phone WPF XAML Zip

  • Entries RSS
  • Comments RSS
©2025 Bruno Sonnino | Design: Newspaperly WordPress Theme
Menu
  • Home
  • About