-
-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathEnumToBoolConverter.cs
More file actions
48 lines (41 loc) · 1.46 KB
/
EnumToBoolConverter.cs
File metadata and controls
48 lines (41 loc) · 1.46 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
using System;
using System.Globalization;
using System.Windows.Data;
namespace ListViewSample
{
[ValueConversion(typeof(ListViewMode), typeof(bool))]
public sealed class EnumToBoolConverter : IValueConverter
{
/// <summary> Gets the default instance </summary>
public static readonly EnumToBoolConverter Default = new();
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is Enum enumVal && parameter is Enum paramVal)
{
var equal = System.Convert.ToInt64(paramVal) == 0
? System.Convert.ToInt64(enumVal) == 0
: enumVal.HasFlag(paramVal);
return equal;
}
return Binding.DoNothing;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return Equals(true, value)
? parameter
: ((parameter != null) ? DefaultEnumValue(parameter.GetType()) : Binding.DoNothing);
}
public static object DefaultEnumValue(Type enumType)
{
if (enumType != null)
{
if (enumType.IsEnum)
{
return Enum.GetValues(enumType).GetValue(0);
}
throw new ArgumentException("given type is not an enum");
}
return null;
}
}
}