WPF did you know series #3: the following code snippets are the same

Snippet 1:

 

Snippet 2:

 

Snippet 3:

Snippet 1 uses in-built converters to convert “special” type of string to a type of Color. It is special because “White” in this code is automatically converted to type of Color.

Snippet 2, in the same vein, has converters that knows how to convert the string “255” to its equivalent Color type.  This code will normally be used if there were no type converters for Color.

Snippet 3 is the rather in the not-often-used category. It’s a good insight to know that we can use Type Converters this way in code-behind.

As a side note, type converters enhance the readability of your XAML code.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.