-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathInverseBooleanConverter.cs
More file actions
54 lines (47 loc) · 1.71 KB
/
InverseBooleanConverter.cs
File metadata and controls
54 lines (47 loc) · 1.71 KB
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
namespace Menees.Windows.Presentation
{
#region Using Directives
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Data;
#endregion
/// <summary>
/// A value converter that inverts a boolean value.
/// </summary>
/// <remarks>
/// This is useful when you need to disable a control when something else is enabled.
/// </remarks>
[ValueConversion(typeof(bool), typeof(bool))]
public class InverseBooleanConverter : IValueConverter
{
#region Public Methods
/// <summary>
/// Converts <paramref name="value"/> to a Boolean and inverts it.
/// </summary>
/// <param name="value">Any value supported by <see cref="System.Convert.ToBoolean(object)"/>.</param>
/// <param name="targetType">Must be System.Boolean.</param>
/// <param name="parameter">The parameter is ignored.</param>
/// <param name="culture">The culture is ignored.</param>
/// <returns>The inverse of <paramref name="value"/> as a Boolean.</returns>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
// From: http://stackoverflow.com/questions/1039636/how-to-bind-inverse-boolean-properties-in-wpf
if (targetType != typeof(bool))
{
throw Exceptions.NewInvalidOperationException("The target type must be a Boolean.");
}
bool result = !System.Convert.ToBoolean(value);
return result;
}
/// <summary>
/// Calls <see cref="Convert(object, Type, object, CultureInfo)"/>.
/// </summary>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
=> this.Convert(value, targetType, parameter, culture);
#endregion
}
}