Converters
The Telerik suite provides a set of built-in IValueConverters which can be used to perform conversion between different types.
The converters are located in the Telerik.Windows.Controls.dll assembly.
Example 1: Specifying converters in XAML
<FrameworkElement>
<FrameworkElement.Resources>
<telerik:BooleanToOpacityConverter x:Key="BooleanToOpacityConverter"/>
<telerik:InvertedBooleanToOpacityConverter x:Key="InvertedBooleanToOpacityConverter"/>
<telerik:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
<telerik:InvertedBooleanToVisibilityConverter x:Key="InvertedBooleanToVisibilityConverter"/>
<telerik:VisibilityToBooleanConverter x:Key="VisibilityToBooleanConverter"/>
<telerik:EnumToVisibilityConverter x:Key="EnumToVisibilityConverter"/>
<telerik:ColorToBrushConverter x:Key="ColorToBrushConverter"/>
<telerik:NullToVisibilityConverter x:Key="NullToVisibilityConverter"/>
<telerik:DateTimeTypeConverter x:Key="DateTimeTypeConverter"/>
<telerik:UppercaseConverter x:Key="UppercaseConverter"/>
<telerik:BinaryImageConverter x:Key="BinaryImageConverter"/>
<telerik:DoubleToThicknessConverter x:Key="DoubleToThicknessConverter"/>
<telerik:OpacityConverter x:Key="OpacityConverter"/>
<telerik:ThicknessToOrientedThicknessConverter x:Key="ThicknessToOrientedThicknessConverter"/>
<telerik:StringToGlyphConverter x:Key="StringToGlyphConverter"/>
<telerik:BrushToColorConverter x:Key="BrushToColorConverter"/>
</FrameworkElement.Resources>
</FrameworkElement>
BooleanToOpacityConverter
This converter converts Boolean values to 1 or 0 opacity. If the Boolean value is false the converter will return 0 opacity.
InvertedBooleanToOpacityConverter
This converter converts Boolean values to 1 or 0 opacity. If the Boolean value is false the converter will return 1 opacity.
BooleanToVisibilityConverter
This converter converts Boolean values to Visibility enumeration values. If the Boolean value is false the Convert() method will return Visibility.Collapsed.
InvertedBooleanToVisibilityConverter
This converter converts Boolean values to Visibility enumeration values. If the Boolean value is true the Convert() method will return Visibility.Collapsed.
VisibilityToBooleanConverter
This converter converts values from the Visibility enumeration to Boolean values. The Convert() method returns true if the passed value is Visibility.Visible and false otherwise.
To invert the results of the converter, set the IsInverted property. This way, Visibility.Visible will be converted to false and Visibility.Collapsed to true.
Example 2: Setting the view model
<FrameworkElement.Resources>
<telerik:VisibilityToBooleanConverter x:Key="InvertedVisibilityToBooleanConverter" IsInverted="True"/>
</FrameworkElement.Resources>
EnumToVisibilityConverter
Converts an Enum value to Visibility enumeration values. If the enum value is one of the values specified in the converter parameter, the Convert() method will return Visibility.Visible, otherwise it will return Visibility.Collapsed. Multiple enum values could be specified in the converter parameter, separated using ,
or ;
.
Example 3: Setting the view model
public class MainViewModel
{
public Gender Gender { get; set; }
}
public enum Gender
{
Female,
Male
}
Example 4: Setting the converter to the Visibility property
<telerik:RadRadioButton Visibility="{Binding MyGender, Converter={StaticResource EnumToVisibilityConverter}, ConverterParameter=Male}"/>
ColorToBrushConverter
This converter converts Color values to Brush values.
NullToVisibilityConverter
This converter converts Null or empty string values to Visibility enumeration values. If the input value is Null or empty string, the converter will return Visibility.Collapsed.
NumberToVisibilityConverter
This converter converts a number (double or integer) value to Visibility enumeration value. If the value is less than or equal to 0, returns Visibility.Collapsed, otherwise returns Visibility.Visible.
DateTimeTypeConverter
The converter converts string to a DateTime format value.
UppercaseConverter
This converter converts all lowcase to uppercase letters.
BinaryImageConverter
The converter converts byte array to System.Windows.Media.Imaging.BitmapImage object.
DoubleToThicknessConverter
The converter converts numeric value to Thickness based on the parameter.
Example 5: Setting the view model
public class MainViewModel
{
public double Thickness { get; set; }
}
Example 6: Setting the converter to the BorderThickness property
<Border Background="Green" BorderThickness="{Binding Thickness,Converter={StaticResource DoubleToThicknessConverter},ConverterParameter=LeftRight}" BorderBrush="Black"/>
OpacityConverter
The converter applies opacity to a Color or Brush value based on the parameter.
Example 7: Setting the view model
public class MainViewModel
{
public MainViewModel()
{
this.Color = Brushes.Red;
}
public SolidColorBrush Color { get; set; }
}
Example 8: Setting the converter to the Background property
<Border Background="{Binding Color, Converter={StaticResource OpacityConverter},ConverterParameter=8}" />
ThicknessToOrientedThicknessConverter
The converter applies Thickness to a property of type Thickness based on the parameter. The parameter expects string value which represents on which side you want to place border (LeftTopRightBottom). You can specify only two sides for example (LeftTop). The parameter is required.
Example 9: Setting the ViewModel
public class MainWindow
{
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
Value = new Thickness(2, 3, 4, 5);
}
public Thickness Value { get; set; }
}
Example 10: Setting the converter to the BorderThickness property
<Border BorderThickness="{Binding Value,Converter={StaticResource ThicknessToOrientedThicknessConverter},ConverterParameter=LeftTop}" BorderBrush="Red" Width="200" Height="200" Background="Bisque"/>
BrushToColorConverter
The BrushToColorConverter converts a SolidColorBrush object to a Color object. The returned color comes from the Color property of the brush. The converter supports TwoWay bindings. If an empty or invalid value is passed to the Convert() method, the returned result is null
.