Skip to content

Instantly share code, notes, and snippets.

@mniak
Last active July 11, 2018 14:18
Show Gist options
  • Save mniak/bac6bfc08ae3a5fd0caf to your computer and use it in GitHub Desktop.
Save mniak/bac6bfc08ae3a5fd0caf to your computer and use it in GitHub Desktop.
#wpf converters
using System;
using System.Windows;
using System.Windows.Data;
namespace WpfConverters
{
public class BooleanToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (!(value is bool))
return DependencyProperty.UnsetValue;
var result = (bool)value
? Visibility.Visible
: Visibility.Collapsed;
return result;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (!(value is Visibility))
return DependencyProperty.UnsetValue;
var v = (Visibility)value;
var result = v == Visibility.Visible
? true
: false;
return result;
}
}
}
using System;
using System.Collections;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Data;
namespace WpfConverter
{
public class CountConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var val = value as IEnumerable;
if (val == null)
return DependencyProperty.UnsetValue;
var count = val.Cast<object>().Count();
return count;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
}
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
namespace WpfConverters
{
public class EnumToBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string parameterString = parameter as string;
if (parameterString == null)
return DependencyProperty.UnsetValue;
if (Enum.IsDefined(value.GetType(), value) == false)
return DependencyProperty.UnsetValue;
object parameterValue = Enum.Parse(value.GetType(), parameterString);
return parameterValue.Equals(value);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
string parameterString = parameter as string;
if (parameterString == null)
return DependencyProperty.UnsetValue;
return Enum.Parse(targetType, parameterString);
}
}
}
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
namespace WpfConverters
{
public class NegateBooleanConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (!(value is bool))
return DependencyProperty.UnsetValue;
var result = !(bool)value;
return result;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return Convert(value, targetType, parameter, culture);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment