

In my experience 95% of converters are doing very simple things (null checks, to strings, adapting one value type to another, etc.), which are easy to implement inline. So QuickConverter is super useful and can help speed up development time by allowing most, if not all, of your converters to be written inline.
#Wpf colorconverter code#
This post shows the code for how you would traditionally accomplish this. So basically if the property we are binding to is true, we want it to return false, and if it’s false, we want it to return true. A simple inverse boolean converter exampleĪs a simple example, let’s do an inverse boolean converter something that is so basic I’m surprised that it is still not included out of the box with Visual Studio (and why packages like WPF Converters exist). And it’s available on NuGet so it’s a snap to get it into your project. QuickConverter is awesome because it allows you to write C# code directly in your XAML this means no need for creating an explicit converter class. Instead I want to spread the word about a little known gem called QuickConverter. There are lots of tutorials on creating converters, so I’m not going to discuss that in length here. If you’ve used binding at all in WPF then you more then likely have also written a converter. Its a bit ugly, but its a one-time hit.Don't Write WPF Converters Write C# Inline In Your XAML Instead Using QuickConverter Public static Color FromName(string name)įoreach (PropertyInfo property in typeof(Colors).GetProperties())

You could fairly easily fetch the property names and values from once into a map: private static readonly Dictionary KnownColors = FetchColors() If you want to avoid boxing, build a dictionary up to start with for the standard names (still using ColorConverter) and then use the dictionary for subsequent lookups. Call ColorConverter.ConvertFromString and cast the result. Of course, ColorConverter is the way to go. Wpf – How do I convert a string like Red to a ? var result = ColorConverter.ConvertFromString(Red) as Color Wpf – How do I convert a string like Red to a ? var color = (Color)ColorConverter.ConvertFromString(Red)
