Last active
January 16, 2016 19:25
-
-
Save BryanWilhite/a00932fd76b956821904 to your computer and use it in GitHub Desktop.
WPF multi-binding with LINQ: BooleanToVisibilityMultiValueConverter
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.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