Skip to content

Instantly share code, notes, and snippets.

@wi7a1ian
Last active May 20, 2019 10:04
Show Gist options
  • Save wi7a1ian/816a5681363d1ee0cdcd8d568f164255 to your computer and use it in GitHub Desktop.
Save wi7a1ian/816a5681363d1ee0cdcd8d568f164255 to your computer and use it in GitHub Desktop.
Set of useful converters that can be used in XAML #wpf #csharp
using MaterialDesignThemes.Wpf;
using System;
using System.Collections;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace Foo
{
public class InverseBooleanToVisibilityConverter : IValueConverter
{
private BooleanToVisibilityConverter converter = new BooleanToVisibilityConverter();
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var result = converter.Convert(value, targetType, parameter, culture) as Visibility?;
return result == Visibility.Collapsed ? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
var result = converter.ConvertBack(value, targetType, parameter, culture) as bool?;
return result == true ? false : true;
}
}
[ValueConversion(typeof(string), typeof(Visibility))]
public class StringToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (string.IsNullOrEmpty((string)value)) ? Visibility.Collapsed : Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class NullToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value == null ? Visibility.Collapsed : Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class NullToBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value != null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class PassThroughConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return values.ToList();
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
[ValueConversion(typeof(bool), typeof(bool))]
public class InvertBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
bool booleanValue = (bool)value;
return !booleanValue;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
bool booleanValue = (bool)value;
return !booleanValue;
}
}
public class IntToBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (int)value == 0;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class BooleanAndConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return values.OfType<IConvertible>().All(System.Convert.ToBoolean);
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
public class ByteToStringConverter : IValueConverter
{
private const int kilobyte = 1024;
private const int megabyte = 1024* kilobyte;
private const int gigabyte = 1024* megabyte;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string size = "0 Bytes";
if (value != null)
{
try
{
double byteCount = System.Convert.ToDouble(value);
if (byteCount >= gigabyte)
{
size = $"{(byteCount / gigabyte):n0} GB";
}
else if (byteCount >= megabyte)
{
size = $"{(byteCount / megabyte):n0} MB";
}
else if (byteCount >= kilobyte)
{
size = $"{(byteCount / 1024):n0} KB";
}
else if (byteCount < kilobyte)
{
size = $"{byteCount:n0} Bytes";
}
}
catch(Exception)
{
size = "NaN"; // Taken from JavaScript, it means "not a number"
}
}
return size;
}
public static object Convert(object value)
{
return new ByteToStringConverter().Convert(value, null, null, CultureInfo.CurrentCulture);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class DataTypeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value?.GetType();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class EnumDescriptionConverter : IValueConverter
{
private string GetEnumDescription(Enum enumObj)
{
FieldInfo fieldInfo = enumObj.GetType().GetField(enumObj.ToString());
object[] attribArray = fieldInfo.GetCustomAttributes(false);
if (attribArray.Length == 0)
{
return enumObj.ToString();
}
else
{
DescriptionAttribute attrib = attribArray[0] as DescriptionAttribute;
return attrib.Description;
}
}
object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is Enum myEnum)
{
return GetEnumDescription(myEnum);
}
return string.Empty;
}
object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return string.Empty;
}
}
public class EnumToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return ((Enum)value).HasFlag((Enum)parameter);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value.Equals(true) ? parameter : Binding.DoNothing;
}
}
public class BadgeCounterToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
int val = (int)value;
if (val >= 99)
{
return "99+";
}
else if (val == 0)
{
return string.Empty;
}
else
{
return val.ToString();
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment