-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBoolToVisibilityConverter.cs
65 lines (60 loc) · 2.42 KB
/
BoolToVisibilityConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
namespace Sharp.Utils.Wpf
{
/// <summary>
/// Converts a <see cref="System.Boolean"/> value to customizable <see cref="System.Windows.Visibility"/> values.
/// </summary>
[ValueConversion(typeof(bool), typeof(Visibility))]
public sealed class BoolToVisibilityConverter : IValueConverter
{
/// <summary>
/// The Visibility value to use when the source value is <value>true</value>.
/// </summary>
public Visibility TrueValue { get; set; }
/// <summary>
/// The Visibility value to use when the source value is <value>false</value>.
/// </summary>
public Visibility FalseValue { get; set; }
/// <summary>
/// Creates a new <see cref="BoolToVisibilityConverter"/> instance.
/// </summary>
public BoolToVisibilityConverter()
{
TrueValue = Visibility.Visible;
FalseValue = Visibility.Collapsed;
}
/// <summary>
/// Converts a value.
/// </summary>
/// <param name="value">The value produced by the binding source.</param>
/// <param name="targetType">The type of the binding target property.</param>
/// <param name="parameter">The converter parameter to use.</param>
/// <param name="culture">The culture to use in the converter.</param>
/// <returns>The converted value.</returns>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (!(value is bool))
return null;
return (bool)value ? TrueValue : FalseValue;
}
/// <summary>
/// Converts a value.
/// </summary>
/// <param name="value">The value that is produced by the binding target.</param>
/// <param name="targetType">The type to convert to.</param>
/// <param name="parameter">The converter parameter to use.</param>
/// <param name="culture">The culture to use in the converter.</param>
/// <returns>The converted value.</returns>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (Equals(value, TrueValue))
return true;
if (Equals(value, FalseValue))
return false;
return null;
}
}
}