using System; using System.ComponentModel; using System.Globalization; using System.Windows.Data; namespace WPFComponents { [ValueConversion(typeof(string), typeof(int))] public class StringToIntegerConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return System.Convert.ToInt32(value); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return System.Convert.ToString(value); } } }
using System; using System.ComponentModel; using System.Globalization; using System.Windows.Data; namespace WPFComponents { [ValueConversion(typeof(string), typeof(string))] public class StringToLowerCaseConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return System.Convert.ToString(value).ToLower(); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return System.Convert.ToString(value); } } }
using System; using System.ComponentModel; using System.Globalization; using System.Windows.Data; using System.Xml; namespace WPFComponents { [ValueConversion(typeof(XmlElement), typeof(decimal))] public class XmlElementToDecimalConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return System.Convert.ToDecimal(((XmlElement)value).InnerText); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return System.Convert.ToString(value); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Globalization; using System.ComponentModel; using System.Windows.Data; using System.Windows; using System.Windows.Controls; namespace WPFComponents { [ValueConversion(typeof(ListBoxItem), typeof(Thickness))] public class IndexToMarginConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { Thickness ret; int itemIndex; ListBoxItem lstItem; ListBox lstBox; lstItem = (value as ListBoxItem); lstBox = (ListBox)ItemsControl.ItemsControlFromItemContainer(lstItem); itemIndex = lstBox.ItemContainerGenerator.IndexFromContainer(lstItem); if ((itemIndex % 2) != 0) ret = new Thickness(5, 0, 30, 30); else ret = new Thickness(30, 25, 0, 0); return ret; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Globalization; using System.ComponentModel; using System.Windows.Data; using System.Windows; using System.Windows.Controls; namespace WPFComponents { [ValueConversion(typeof(ListBoxItem), typeof(decimal))] public class IndexToAngleConverter : IValueConverter { public const int ANGLE = 15; public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { double ret; int itemIndex; ListBoxItem lstItem; ListBox lstBox; // The 'value' parameter is the ListBoxItem lstItem = (value as ListBoxItem); // Get a reference to the list box so we can get the index of which item is being drawn lstBox = (ListBox)ItemsControl.ItemsControlFromItemContainer(lstItem); // Get the index of the item being drawn itemIndex = lstBox.ItemContainerGenerator.IndexFromContainer(lstItem); if ((itemIndex % 2) != 0) ret = ANGLE; else ret = -(ANGLE); return ret; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Data; namespace Halliburton.Castor.Views.Converter { class DateToDayStringConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { DateTime? date = (DateTime?)value; return date.HasValue ? date.Value.Day.ToString() : "1"; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } }
using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text; using System.Windows.Data; namespace Halliburton.Castor.Views.Converter { class EnumMatchToBooleanConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value == null || parameter == null) return false; string checkValue = value.ToString(); string targetValue = parameter.ToString(); return checkValue.Equals(targetValue, StringComparison.InvariantCultureIgnoreCase); } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { if (value == null || parameter == null) return null; bool useValue = (bool)value; string targetValue = parameter.ToString(); if (useValue) return Enum.Parse(targetType, targetValue); return null; } } }
public class InverseBooleanConverter<bool> : ValueConverter { protected override bool Convert(bool value, Type targetType, object parameter, CultureInfo culture) { return !value; } protected override bool ConvertBack(bool value, Type targetType, object parameter, CultureInfo culture) { return !value; } }
public class BoolToVisibilityConverter<bool,Visibility> : ValueConverter { protected override Visibility Convert(bool value, Type targetType, object parameter, CultureInfo culture) { return value ? Visibility.Visible : Visibility.Collapsed; } protected override bool ConvertBack(Visibility value, Type targetType, object parameter, CultureInfo culture) { return value == Visibility.Visible; } }
Byte Array to Image Converter
ByteToImageConverter will convert byte array of image to a BitmapImage which can be used in Source property of an image. This can be used when we have an image saved in binary form in database and we want to bind that and show in image control. We can show a default image if byte array is null by uncommenting the code in “else” part of BitmapToImageConverter class.
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; } }