Using value converters with parameters
As you saw in the previous recipe, a value converter allows you to convert one value into another, with the same or different type. However, such a solution also makes it possible to take into account a parameter during a conversion. Thus, you can prepare one value converter and parameterize it directly in the XAML code to limit code duplication and improve its quality. You will learn how to do this in this recipe.
As an example, you will create a page with a button and a text block. After consecutive clicks on the button, the color of the text block should be changed to green, red, green, and so on. Of course, the example will use the data binding mechanism with a value converter. This converter takes a parameter with a string representation of two colors for the various states of a Boolean value, such as #db0707|#33b11a
. Thus, the first color will be returned when the Boolean value is equal to true
. Otherwise, the second color will be used.