41 lines
1.6 KiB
C#
41 lines
1.6 KiB
C#
using System;
|
|
using System.Windows;
|
|
using System.Windows.Data;
|
|
|
|
namespace JD.WPF.Converters
|
|
{
|
|
public class BoolToVisibilityConverter : IValueConverter
|
|
{
|
|
public Visibility VisibilityWhenNotDisplayed { get; set; }
|
|
|
|
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
|
|
{
|
|
if (!(value is bool))
|
|
throw new ArgumentException("Converter value must be of type Boolean");
|
|
if (!(parameter is bool))
|
|
throw new ArgumentException("Converter parameter must be of type Boolean");
|
|
|
|
bool boolValue = (bool)value;
|
|
bool invertTest = (bool)parameter;
|
|
if (boolValue)
|
|
return invertTest ? Visibility.Visible : VisibilityWhenNotDisplayed;
|
|
else
|
|
return invertTest ? VisibilityWhenNotDisplayed : Visibility.Visible;
|
|
}
|
|
|
|
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
|
|
{
|
|
if (!(value is Visibility))
|
|
throw new ArgumentException("Converter value must be of type System.Windows.Visibility");
|
|
if (!(parameter is bool))
|
|
throw new ArgumentException("Converter parameter must be of type Boolean");
|
|
|
|
Visibility visibility = (Visibility)value;
|
|
bool invertTest = (bool)parameter;
|
|
if (visibility == Visibility.Collapsed || visibility == Visibility.Hidden)
|
|
return invertTest ? false : true;
|
|
else
|
|
return invertTest ? true : false;
|
|
}
|
|
}
|
|
}
|