Converters

Overview

Silverlight data binding engine allows data to be bound through XAML or programmatically with code. Sometimes you want to databind two properties that have incompatible types. You need some code that converts the value from source to target type and back. This code is called ValueConverter. Basically a Value Converter is a class, that implements the interface IValueConverter interface.

The following converters are included in this toolkit: Boolean to Visibility, String To Visibility, Themed Image Converter, Inversed Theme Image Converter, Visibility to Boolean.
To begin using these converters first add a reference to the Coding4Fun.Phone.Controls.dll assembly. To use them in XAML you have to add the following namespace declaration:

xmlns:Controls="clr-namespace:Coding4Fun.Phone.Controls.Converters;assembly=Coding4Fun.Phone.Controls"

XAML:
<phone:PhoneApplicationPage.Resources>
    <Controls:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
    <Controls:VisibilityToBooleanConverter x:Key="VisibilityToBooleanConverter"/>
    <Controls:ThemedImageConverter x:Key="ThemedImageConverter"/>
    <Controls:ThemedInverseImageConverter x:Key="ThemedInverseImageConverter"/>
</phone:PhoneApplicationPage.Resources>

C#:
BooleanToVisibilityConverter booleanToVisibilityConverter = new BooleanToVisibilityConverter();
VisibilityToBooleanConverter visibilityToBooleanConverter = new VisibilityToBooleanConverter();
ThemedImageConverter themedImageConverter = new VisibilityToBooleanConverter();
ThemedInverseImageConverter  themedInverseImageConverter  = new VisibilityToBooleanConverter();


Boolean to Visibility

Use the BooleanToVisibilityConverter class to convert a Boolean to and from a Visibility value.
Example:
<CheckBox x:Name="checkBox"/>
<TextBlock Text="CheckBox is Checked" Visibility="{Binding ElementName=checkBox, Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter} }"/>



String To Visibility

This converter converts a string to Visibility.

Themed Image Converter

This converter is an Image converter.
Example:
<Button>
    <Image Stretch="None" Source="{Binding Converter={StaticResource ThemedImageConverter}, ConverterParameter={StaticResource PhoneBackgroundColor} }"
           DataContext="/WP7SampleProject4;component/Images/{0}/appbar.feature.camera.rest.png" />
</Button>



Inversed Theme Image Converter

This converter is almost the same as ThemedImageConverter but it inverses the image.
Example:
<Button Background="YellowGreen">
    <Image Stretch="None" Source="{Binding Converter={StaticResource ThemedInverseImageConverter}, ConverterParameter={StaticResource PhoneBackgroundColor} }"
            DataContext="/WP7SampleProject4;component/Images/{0}/appbar.feature.camera.rest.png" />
</Button>



Visibility to Boolean

Represents the converter that converts Visibility enumeration values to and from Boolean values.
Example:
<CheckBox x:Name="checkBox1" IsChecked="{Binding ElementName=text, Path=Visibility, Converter={StaticResource VisibilityToBooleanConverter} }"/>
<ToggleButton x:Name="togglebutton" Content="Check/Uncheck CheckBox" />
<TextBlock x:Name="text" Text="ToggleButton Pressed -> text is Visible" Visibility="{Binding ElementName=togglebutton, Path=IsChecked, Converter={StaticResource BooleanToVisibilityConverter} }"/>

Last edited Apr 24, 2011 at 5:59 PM by crutkas, version 2

Comments

system16 Oct 18, 2013 at 12:33 PM 
I believe the namespace has been changed:

xmlns:Controls="clr-namespace:Coding4Fun.Toolkit.Controls.Converters;assembly=Coding4Fun.Toolkit.Controls"

bc3tech Jun 22, 2013 at 3:39 PM 
Something I often put in my own implementations of BooleanToVisibility is the handling of a 'reverse' parameter, so that Boolean 'true' -> Collapsed if you pass 'true' for the parameter value of the converter.

Just a thought to give a little more robustness to the converter