Reference no: EM132175752
Value Converters in WPF C#
We often use data binding to display primitive data (ints, strings, even bools) directly to the UI. However, what if we want to bind a value to a dependency property of an incompatible type? Or what if the data value needs to be translated before it is displayed? WPF gives us the value converters in order to meet this need.
Let's build a color hex value translator.
Requirements
You will build a UI with the following qualities:
There will be eight text boxes. Four of them will represent the decimal values of the current color (each between 0 and 255 inclusive). The remaining four will be the hexadecimal values of the current color (each between 00 and FF).
You must build a custom value converter to convert between the decimal and hexadecimal values of each box. Changing the hex Red box should update the decimal Red box and vice versa.
There will be a checkbox for the option "Display Preview With Sliders". Clicking the checkbox toggles the visibility of a color control with 4 sliders and a preview panel.
While you will need a value converter for this, there is no need to build a custom one. You will use WPF's built-in value converter class BooleanToVisibilityConverter.
The preview panel will be bound to its sliders in order to display the currently selected color in real time.
You will create a custom multi-converter and use it in a MultiBinding in order to accomplish this.
The sliders themselves should be clearly labeled, slide only in whole number values, span a range of values from 0 to 255 inclusive, and have a non-editable label displaying the its current value.
Finally, you will bind the sliders of the preview control to the original decimal text boxes using two-way binding.