DEV Community 👩‍💻👨‍💻

Julien Amsellem
Julien Amsellem

Posted on • Originally published at

An alternative to Xaml for WPF


FluentWPF is a library which offers an alternative Xaml.


  • Reduce verbosity
  • Provide more flexibility
  • Improve reusability
  • Leverage tools and C# langage capabilities
  • Ease unit testing


  • Provide a fluent API
  • Make this library easily extensible
  • Reuse existing WPF types
  • Provide many sample usages


Let's see what it looks like with some piece of code.

Simple window

new Window()
  .DataContext(new RootViewModel())

I hope the code is pretty straitforward but let's write the xaml equivalent:

   <local:RootViewModel />

Alignment is easy

new Expander()
  .Size(400, 400)

Indeed it's as easy as to align Left or Right or Center or Bottom.

Set a property

new TextBlock()
  .Set(FrameworkElement.MarginProperty, new Thickness(4))
  .Set(TextBlock.TextProperty, "Some text"))

Experiment with bindings

new Button()

The binding API should greatly simplify binding syntax. Sure you can bind OneWay or TwoWay or OneWayToSource or OneTime.
You can define the binding source using With and specify the path to the DataContext property or bind to self using WithSelf.

Bindings and converters

new ProgressBar()
    .Convert(x => Math.Round((double) x)))

If you want to adapt your viewmodel's property to your view you still can provide a IValueConverter but you can also provide a lambda expression or a method and keep it simple.

Create a grid and populate it

new Grid()
  .DefaultCellSize("*", "*")
    .Contains(new Button()
    .Contains(new Button()

The grid API is meant to ease dealing with grids. For exemple you don't need to first declare rows and columns.
And you can define default width and heigh and specify a specific value for any cell if wanted.
The sample above only demonstrate a very small part of the Grid API

Simple style creation

Style aCheckBox = StyleExtensions.Create()
    .Set(Control.ForegroundProperty, new SolidColorBrush(Colors.White))
    .Set(Control.BackgroundProperty, new SolidColorBrush(Colors.Gray))

Which is equivalent to:

<Style x:Key="ACheckBox" TargetType="CheckBox">
  <Setter Property="Foreground" Value="White" />
  <Setter Property="Background" Value="Gray" />

Well is this very simple exemple FluentWPF is not shorter and even a little bit more verbose.
But wait, the Triggers API and the Theme API will greatly help !

Styles with triggers

Style aCheckBox = StyleExtensions.Create()
  .Set(Control.ForegroundProperty, new SolidColorBrush(Colors.Gray))
    .Then(Control.FontWeightProperty, FontWeights.Bold)
    .Then(Control.ForegroundProperty, new SolidColorBrush(Colors.White))

Which is equivalent to:

<Style x:Key="ACheckBox" TargetType="CheckBox">
  <Setter Property="Foreground" Value="White" />
    <Trigger Property="IsChecked">
      <Setter Property="FondWeight" Value="Bold" />
      <Setter Property="Foreground" Value="White" />

Simple ControlTemplate

ControlTemplate template = TemplateExtensions.Create<Border>()
  .Set(FrameworkElement.HorizontalAlignmentProperty, HorizontalAlignment.Stretch)

This is a very simple ControlTemplate for ListBoxItem elements.

DataTemplate is easy too

DataTemplate template = TemplateExtensions.Create<Border>()
  .Set(FrameworkElement.HorizontalAlignmentProperty, HorizontalAlignment.Stretch)

You can use the same API to create a DataTemplate as for ControlTemplate.

ControlTemplate with TemplateBinding

ControlTemplate template = TemplateExtensions.Create<Border>()
  .Set(FrameworkElement.HorizontalAlignmentProperty, HorizontalAlignment.Stretch)
  .TemplateBinding(Border.BackgroundProperty, ListBoxItem.BackgroundProperty)

Theming ?

A work in progress will also provide a fluent API to ease theme creation.
The API will:

  • provide a way to define a color palette
  • provide an easy way to define default style for any control type
  • take care of resource dictionary loading


  • Cover same functionalities as XAML
    • Support templates/styles
    • Support bindings/converters
    • Support animations
    • Support triggers
    • Support behaviors
  • Write samples
  • Offer a code friendly Theme engine
  • More samples
  • Provide Visual studio code snippets
  • API documention (or more samples)
  • Extend the API functionalities beyond XAML
    • Leverage lambdas (for triggers?)
    • Support inheritance/mixins for styles
  • Further reduce API verbosity


Right now FluentWPF is not finished and not published, but I'd love to get feedback from the community about this project.
I plan to release FluentWPF as a library bundled with a quite advanced demo application.
The license is not defined and I'm not yet sure to open source it or not.

Top comments (4)

jayjeckel profile image
Jay Jeckel

Nice! Anything is better than XAML. When I have to use WPF instead of WinForms, I'll definitely be giving this library a try.

themulti0 profile image

This library seems very nice and useful, and I might use it in some simple projects!

rosberg_guedes profile image
Rosber Guedes

Why would you use this instead of a simple XAML?

julienju2pom profile image
Julien Amsellem Author


The short answer is at the top of this article.
Long answer:

  • I wanted to get a way to put more advanced behavior in the UI without the hurdle of Converters and Behaviors
  • Keep MVVM pattern
  • Play with fluent API because I love it :)
  • Get rid of the xaml editor (which seems to improve with time)
  • Be able to reuse any piece of code (I know that xaml allows reusability too)
  • I'm also curious to see if there's any positive performance impact either during compilation time or runtime (but I didn't measure it yet)
  • Some people knows C# well but not xaml, so it can be easier for them to develop UI using this library

🌚 Browsing with dark mode makes you a better developer by a factor of exactly 40.

It's a scientific fact.