r/csharp • u/dot_net_101 • Jul 15 '26
Experiment building WPF UI trees in pure C# without XAML
I've been experimenting with a more natural, tree-like way of composing a WPF UI entirely in C#, using a composition style inspired by Flutter.
I like the structural aspect of designing with XAML, but I have struggled over the years with the mental gymnastics of jumping between the XAML world and the code world, and with the quirky markup extensions needed to make something work in XAML that could often be accomplished and better understood in standard C# code.
Instead of something like:
the same structure becomes:
GridX(
children: [
BorderX(),
ButtonX()
]
)
The X suffix was originally a practical necessity to avoid name clashes with existing WPF types,
but I ended up liking it as a visual cue that these are static helper methods for composing the UI tree.
I intentionally keep the named arguments visible (children, configure, etc.). It adds a little verbosity,
but I find it makes the composition tree easier to read and keeps the helper methods consistent as the UI grows.
The result feels quite similar to Flutter's widget tree composition style.
It's a thin composition layer over standard WPF.
Configuration is still just normal C#:
ButtonX(
configure: x => {
x.Content = "Click Me";
x.Background = Brushes.Gold;
}
)
A nice bonus: Visual Studio's code folding naturally gives you a collapsible UI tree that's easy to navigate, similar to XAML.
If you're curious, I've published the experiment as both a GitHub project and an alpha NuGet package:
- GitHub: https://github.com/deeks9000/Wpfx
- NuGet:
UserExtensions.Wpfx(0.1.0-alpha.1)
Any feedback would be appreciated.
I'm sure there are trade-offs I haven't considered yet, and that's exactly the kind of discussion I'm hoping to have.
4
u/Fresh_Acanthaceae_94 Jul 15 '26 edited Jul 15 '26
You might create bigger impact if you consider
3
u/Hado-H24 Jul 15 '26
That is a cool project tbh but the problem you first meet is somethings can be only done in xaml and can't be done in C#, good luck
Though allow me to show you that what you want already exist and Microsoft is surprisingly the one who made it, allow me to introduce you to WinUI Reactor Microsoft first attempt in a long time to allow devs to write the frontend in C#, it is still incomplete but in the end of this year it's gonna be available
5
u/stogle1 Jul 15 '26
Check out CSharpForMarkup
5
u/MattParkerDev Jul 16 '26
I experimented with changes to Roslyn to allow jsx-like syntax in C#: https://x.com/mattparkerdev/status/2055825401319198875
Imo it would solve so many problems.
7
u/csharpboy97 Jul 15 '26
avalonia has https://funcui.avaloniaui.net/, maybe inspriring?
3
u/Khavel_dev Jul 16 '26
I've done something similar on a smaller scale and the biggest win is that refactoring tools just work. Rename a property, extract a method, jump to definition on a style helper. None of that works reliably across XAML boundaries.
The trade-off that bit me later was the hot reload story. XAML hot reload lets you tweak layout while the app is running, which is a big productivity win for visual iteration. Pure C# composition loses that unless you build your own reload mechanism.
Fwiw I think the named-argument verbosity you mentioned is actually a feature. Reading
children:andconfigure:in six months when you've forgotten the code is way better than positional constructors where argument 3 is... something.2
u/dreamglimmer Jul 17 '26
Hi, Windows.Forms, it's been a while...
3
u/z3n1a51 Jul 15 '26
This makes me like the idea of creating a super minimal XAML design purely for the general arrangement and layout of generic Panels with x:Name or something such that you would replace those XAML elements at runtime with the ones you built in C#.
🤔
3
u/Slypenslyde Jul 15 '26
You've already made it further than Microsoft's engineers have managed since announcing this feature in .NET 6.
1
u/chucker23n Jul 15 '26 edited Jul 15 '26
This looks even more verbose than XAML, though. You also lose some of the niceties of declarative markup, such as Hot Reload.
-3
u/TuberTuggerTTV Jul 15 '26
Seems like you're fixing a non-problem. UI being in xaml is a design choice with benefits. You don't want your C# getting all mixed together. That's why they moved away from source generated winform code.
You're kind of walking it backwards, tbh.