Byte Array to Image Converter
public class ByteToImageConverter : IValueConverter { public BitmapImage ConvertByteArrayToBitMapImage(byte[] imageByteArray) { BitmapImage img = new BitmapImage(); using (MemoryStream memStream = new MemoryStream(imageByteArray)) { img.SetSource(memStream); } return img; } public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { BitmapImage img = new BitmapImage(); if (value != null) { img = this.ConvertByteArrayToBitMapImage(value as byte[]); } else { //img = new BitmapImage(new Uri("/AssemblyName;component/Images/defaultImage.jpg", UriKind.Relative)); img = null; } return img; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return null; } }
Example
<Image Margin="3" Source="{Binding Path=ByteArray, Converter={StaticResource byteToImageConverter}}"/>
Null or Empty Visibility Converter
public class NullEmptyVisibilityConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value == null) { return Visibility.Collapsed; } else if (value.GetType() == typeof(string) && string.IsNullOrWhiteSpace(value.ToString()) == true) { return Visibility.Collapsed; } else { return Visibility.Visible; } } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new Exception("Not implemented"); } }
Example
<TextBlock Margin="3" Text="{Binding Path=Data, Converter={StaticResource nullVisibilityConverter}}"/>
Negative Converter
Public Class NegativeConverter Implements IValueConverter Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert If value.[GetType]() Is GetType(Boolean) Then Dim result As Boolean = CBool(value) Return Not result Else Return value End If End Function Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack Throw New Exception("Not implemented") End Function End Class
Example
<StackPanel Orientation="Vertical"> <CheckBox HorizontalAlignment="Left" Margin="3" Width="100" Height="25" Name="chkFirst"/> <CheckBox Name="chkSecond" HorizontalAlignment="Left" Margin="3" Height="25" IsChecked="{Binding Path=IsChecked, ElementName=chkFirst, Converter={StaticResource negativeConverter}}"/> </StackPanel>
Multiplication Converter
Public Class MultiplyConverter Implements IValueConverter Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert If parameter IsNot Nothing Then Dim result As Double = Double.Parse(parameter.ToString()) Return CDbl(value) * result Else Return CDbl(value) End If End Function Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack Throw New Exception("Not implemented") End Function End Class
Example
<StackPanel Orientation="Vertical"> <TextBox HorizontalAlignment="Left" Margin="3" Width="100" Height="25" Name="txtFirst"/> <TextBox Name="txtSecond" HorizontalAlignment="Left" Margin="3" Height="25" Width="{Binding Path=ActualWidth, ElementName=txtFirst, Converter={StaticResource multiplyConverter}, ConverterParameter=2.0}"/> </StackPanel>
Divide Converter
Public Class DivideConverter Implements IValueConverter Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert If parameter IsNot Nothing Then Dim result As Double = Double.Parse(parameter.ToString()) If result > 0 Then Return CDbl(value) / result Else Return CDbl(value) End If Else Return CDbl(value) End If End Function Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack Throw New Exception("Not implemented") End Function End Class
Example
<StackPanel Orientation="Vertical"> <TextBox HorizontalAlignment="Left" Margin="3" Width="100" Height="25" Name="txtFirst"/> <TextBox Name="txtSecond" HorizontalAlignment="Left" Margin="3" Height="25" Width="{Binding Path=ActualWidth, ElementName=txtFirst, Converter={StaticResource divideConverter}, ConverterParameter=2.0}"/> </StackPanel>
Subtract Converter
Public Class SubtractConverter Implements IValueConverter Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert If parameter IsNot Nothing Then Dim result As Double = Double.Parse(parameter.ToString()) Return CDbl(value) - result Else Return CDbl(value) End If End Function Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack Throw New Exception("Not implemented") End Function End Class
Example
<StackPanel Orientation="Vertical"> <TextBox HorizontalAlignment="Left" Margin="3" Width="100" Height="25" Name="txtFirst"/> <TextBox Name="txtSecond" HorizontalAlignment="Left" Margin="3" Height="25" Width="{Binding Path=ActualWidth, ElementName=txtFirst, Converter={StaticResource subtractConverter}, ConverterParameter=15.0}"/> </StackPanel>
Now in SubtractConverter class we would have ActualWidth of txtFirst in value parameter and “15.0” in “parameter” parameter. So we will subtract 15 from ActualWidth and return the result.
Note:
In all the above converters we have to create it’s instance in resource and reference it using their key. For example you can write following code to create instance of SubtractConverter.<Window.Resources> <local:SubtractConverter x:Key="subtractConverter" /> </Window.Resources>