Skip to content

Instantly share code, notes, and snippets.

@BryanWilhite
Last active January 16, 2016 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BryanWilhite/a00932fd76b956821904 to your computer and use it in GitHub Desktop.
Save BryanWilhite/a00932fd76b956821904 to your computer and use it in GitHub Desktop.
WPF multi-binding with LINQ: BooleanToVisibilityMultiValueConverter
using System;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Data;
namespace LexusDashboard.ValueConverters
{
public sealed class BooleanToVisibilityMultiValueConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var flag = false;
if (values == null) return Visibility.Collapsed;
if (values.All(i => i is bool))
{
flag = values.OfType<bool>()
.Aggregate((x, y) => x && y);
}
else if (values.All(i => i is bool?))
{
flag = values.OfType<bool?>()
.Select(i => i.GetValueOrDefault())
.Aggregate((x, y) => x && y);
}
if (parameter != null)
{
if (bool.Parse((string)parameter))
{
flag = !flag;
}
}
if (flag)
{
return Visibility.Visible;
}
else
{
return Visibility.Collapsed;
}
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
var back = ((value is Visibility) && (((Visibility)value) == Visibility.Visible));
if (parameter != null)
{
if ((bool)parameter)
{
back = !back;
}
}
return new[] { (object)back };
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment