4 Tips to increase productivity with WPF Converters

When starting with WPF, I had a hard time realizing when to use converters and just how powerful they can be. I would often create styles with complicated Data Triggers, or abuse my ViewModel instead of doing some simple converter magic.

I’ll share with you some converter tips and tricks that make my development easier every day.

Tip #1: When starting a new project, get a converters library

A  lot of standard converters are reused in every WPF application. No need to recode the wheel. There are several open source libraries available we can use:

Tip #2: Write C# code in XAML with QuickConverter

This is a real gem. This library actually allows to write c# code in XAML.
Here’s an example:

Source: http://blog.danskingdom.com/?s=quickconverter

Or:

In this example the FontSize is changing according to Message’s length. If there’s over 300 characters, the FontSize will be smaller.

I used it in our code base and I think it’s awesome. It’s more readable and we need much less code for simple logic as in the examples above.

For documentation check out this post, the documentation on CodePlex and QuickConverter on NuGet.

This doesn’t cancel out regular converters. We still would want a lot of reusable converters as regular C# classes.

Tip #3: Markup extension BaseConverter

Using a converter requires you to create an instance of the converter. The conventional way is using a StaticResource:

In the above example, we will use the same instance of the converter every time we use StaticResource. This is both good and bad.
Good because we might gain in performance for not having to create a new instance.
Bad because if we want to use parameters with out converter, those parameters will be the same every time we use the StaticResource.

Consider having the converters derive from MarkupExtension. We will be able to write like this:

Our converter will have to derive from MarkupExtension:

This is easier to read and will create a new instance of the converter every time. Which is usually what I want. I want to be able to give different parameters to my converter every time and the performance loss of creating a new instance is negligible.

Here’s a nice post that explains about converters as MarkupExtension in more detail.

Tip #4 Result Converter

Sometimes, we need to do a “double” conversion. Or, in other words, convert the result of our conversion.

Let’s use the example from Tip #2. In a TextBlock, we want to use a smaller font if the message is too long (More than 300 characters).

We saw how to do this easily with QuickConverter, but let’s assume we want to do it the old fashion way for whatever reasons.

Instead of creating a new custom converter, we can save some time and effort and use our standard converters. Like these two:

  • StringLength converter: Returns number of characters in a string
  • EqualityConverter: Accepts 3 parameters
    • ToCompare
    • Smaller:  Value when Binding < ToCompare
    • Bigger: Value when Binding >= ToCompare

Here’s the code that combines the above standard converters to our custom needs:

To be able to do that, we need to add ResultConverter to our BaseConverter class. Like this:

This is really convenient, trust me. It will save time writing those extra unnecessary converters.

A note about the BaseConverter class:

In the last example we saw a BaseConverter that derives from MarkupExtension and uses ResultConverter. It is missing the methods ConvertBack and ConvertBackInternal to make the base class usable.

Also, I suggest having a base class for MultiValueConverer that has the same concepts.

Summary

I showed you some tricks that make my WPF development more productive and the code more readable.

I hope I gave you some value and maybe made converters look a bit more useful and easier.

Have a great day,
Michael

Leave a Reply