Language: EN

csharp-wpf-extensions

WpfExtension, utilities for applications in WPF

WpfExtensions is a .NET library 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 streamline the WPF application development process.

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

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

How to use WpfExtensions

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

Install-Package H.InputSimulator

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

Command

The Command markup extension allows us to use a Command pattern without actually having to create the Command (a lot of Command for a single phrase, I’m sorry).

Thus, we can associate the Command of a control,

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

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

class ViewModel
{
    public void Execute() {}
}

Compose

With Compose, we can 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}" />

If

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

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

Switch

With this extension, we can conditionally bind a property to N, similar to how we would do 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}}}" />

WpfExtensions.Binding

It also has a mechanism to improve the reactivity of computed properties. Personally, I am 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);

WpfExtensions is Open Source, and all the code and documentation is available in the project’s repository at https://github.com/DingpingZhang/WpfExtensions