Created
January 11, 2014 23:37
-
-
Save jtheisen/8378447 to your computer and use it in GitHub Desktop.
Xaml's most popular converter.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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