Skip to content

Instantly share code, notes, and snippets.

@JohanLarsson
Last active March 15, 2016 14:40
Show Gist options
  • Save JohanLarsson/c165925b768bc6ec8c0d to your computer and use it in GitHub Desktop.
Save JohanLarsson/c165925b768bc6ec8c0d to your computer and use it in GitHub Desktop.
[MarkupExtensionReturnType(typeof(IValueConverter))]
public class BooleanToVisibilityConverter : MarkupExtension, IValueConverter
{
public Visibility WhenTrue { get; set; } = Visibility.Visible;
public Visibility WhenFalse { get; set; } = Visibility.Collapsed;
public Visibility WhenNull { get; set; } = Visibility.Collapsed;
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool)
{
return Equals(value, true) ? WhenTrue : WhenFalse;
}
return WhenNull;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException("Only supports one way bindings");
}
}
<DataGrid Visibility="{Binding HasItems,
RelativeSource={RelativeSource Self},
Converter={local:BooleanToVisibilityConverter WhenTrue=Collapsed, WhenFalse=Visible}}" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment