csharp-wpf-extensions

WpfExtension, utilities for applications in WPF

  • 2 min

WpfExtensions is a library for .NET that offers some tools to improve the development of WPF applications in C#.

This library was created by Dingping Zhang and aims to simplify and speed up the WPF application development process.

One of WPF’s strong points was the improvement of bindings compared to the previous solution in Winforms. However, it cannot compare to more modern solutions like VueJS.

With WpfExtensions, binding is “a little less horrible,” thanks to some new tools that improve binding syntax, saving you time and allowing you to generate cleaner and simpler code.

How to Use WpfExtensions

We can add the library to a .NET project easily, through the corresponding Nuget package.

Install-Package H.InputSimulator

Here are some examples of how to use WpfExtensions taken from the library’s documentation.

Command

The Command markup extension allows us to use a Command pattern without actually having to create the Command (that’s a lot of “Command” in one sentence, sorry).

Thus, we can associate a control’s Command,

<Element Command={markup:Command Execute} />
Copied!

Directly with a synchronous or asynchronous method, without having to create the Command.

class ViewModel
{
    public void Execute() {}
}
Copied!

Compose

With Compose we can concatenate (chain) converters, to create more complex behaviors without having to create a specific converter.

<TextBlock Visibility="{Binding DescriptionText, Converter={markup:Compose
                       {StaticResource IsNullOrEmptyOperator},
                       {StaticResource NotConverter},
                       {StaticResource BooleanToVisibilityConverter}}}"
           Text="{Binding DescriptionText}" />
Copied!

If

With the IF extension we can conditionally bind a property to two others, based on the value of a third boolean parameter.

<Button Command="{markup:If {Binding BoolProperty},
                            {Binding OkCommand},
                            {Binding CancelCommand}}" />
Copied!

Switch

With this extension we can conditionally bind a property to N others, similar to how we would with a Switch.

<Image Source="{markup:Switch {Binding FileType},
                              {Case {x:Static res:FileType.Music}, {StaticResource MusicIcon}},
                              {Case {x:Static res:FileType.Video}, {StaticResource VideoIcon}},
                              {Case {x:Static res:FileType.Picture}, {StaticResource PictureIcon}},
                              ...
                              {Case {StaticResource UnknownFileIcon}}}" />
Copied!

WpfExtensions.Binding

It also has a mechanism to improve the reactivity of computed properties. Personally, I’m used to using Fody or ReactiveUI to deal with this, so I don’t find it as useful. But there it is, the functionality.

// View Model is derived from WpfExtensions.Binding.BindableBase.
public double Width {
    get => field;
    set => SetProperty(ref field, value);
}

public double Height {
    get => field;
    set => SetProperty(ref field, value);
}

public double RectArea => Computed(() => Width * Height);
Copied!