Skip to content

Instantly share code, notes, and snippets.

@jtheisen
Created January 11, 2014 23:37
Show Gist options
  • Save jtheisen/8378447 to your computer and use it in GitHub Desktop.
Save jtheisen/8378447 to your computer and use it in GitHub Desktop.
Xaml's most popular converter.
using System;
using System.Windows;
using System.Windows.Data;
using System.ComponentModel;
namespace MonkeyBusters
{
public class VisibilityConverter : IValueConverter
{
public Boolean TrueIsVisible { get; set; }
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (DesignerProperties.IsInDesignTool) return Visibility.Visible;
bool b;
if (value is int)
{
b = (int)value > 0;
}
else if (value is bool)
{
b = (bool)value;
}
else if (value == null)
{
return GetVisbility(false);
}
else if (value.GetType().IsClass)
{
return GetVisbility(true);
}
else
{
throw new NotImplementedException(String.Format("Type {0} is not valid for this conversion.", value.GetType().Name));
}
return GetVisbility(b);
}
Visibility GetVisbility(Boolean b)
{
return (b == TrueIsVisible) ? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment