Skip to content

Instantly share code, notes, and snippets.

@YoungjaeKim
Created August 19, 2013 02:07
Show Gist options
  • Save YoungjaeKim/6265240 to your computer and use it in GitHub Desktop.
Save YoungjaeKim/6265240 to your computer and use it in GitHub Desktop.
{Binding Converter={StaticResource BoolConverter}, ConverterParameter='!'} 식으로 사용하면 결과를 반대로 만들어줍니다.
/// <summary>
/// Value converter that translates true to <see cref="Visibility.Visible"/> and false to
/// <see cref="Visibility.Collapsed"/>.
/// </summary>
public sealed class BooleanToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
var visible = (value is bool && (bool)value) ? Visibility.Visible : Visibility.Collapsed;
if (parameter == null)
return visible;
// input whatever thing (such as '!') in XAML converter parameter will change its visiblity output.
if (parameter.ToString().Length > 0)
return visible == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
return visible;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
var result = value is Visibility && (Visibility)value == Visibility.Visible;
if (parameter == null)
return result;
if (parameter.ToString().Length > 0)
return !result;
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment