-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathEnumValuesExtension.cs
97 lines (85 loc) · 2.81 KB
/
EnumValuesExtension.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#region Copyright Syncfusion® Inc. 2001-2025.
// Copyright Syncfusion® Inc. 2001-2025. All rights reserved.
// Use of this code is subject to the terms of our license.
// A copy of the current license can be obtained at any time by e-mailing
// licensing@syncfusion.com. Any infringement will be prosecuted under
// applicable laws.
#endregion
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Markup;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Syncfusion.DemosCommon.WinUI
{
/// <summary>
/// A markup extension that returns a collection of values of a specific <see langword="enum"/>
/// </summary>
[MarkupExtensionReturnType(ReturnType = typeof(List<string>))]
public sealed class EnumToStringValuesExtension : MarkupExtension
{
/// <summary>
/// Gets or sets the <see cref="System.Type"/> of the target <see langword="enum"/>
/// </summary>
public Type Type { get; set; }
/// <inheritdoc/>
protected override object ProvideValue()
{
var values = new List<string>();
foreach (var item in Enum.GetValues(Type))
{
if (!values.Contains(item.ToString()))
{
values.Add(item.ToString());
}
}
return values;
}
}
/// <summary>
/// A Converter class that helps to return enum or string value
/// </summary>
public class StringToEnumValueConverter : MarkupExtension, IValueConverter
{
/// <summary>
/// Gets or sets the a value indicating whether return string or enum
/// </summary>
public bool IsInversed { get; set; }
/// <summary>
/// Gets or sets the <see cref="System.Type"/> of the target <see langword="enum"/>
/// </summary>
public Type Type { get; set; }
/// <inheritdoc/>
protected override object ProvideValue()
{
return this;
}
/// <inheritdoc/>
public object Convert(object value, Type targetType, object parameter, string language)
{
if (IsInversed)
{
if (Enum.TryParse(Type, value?.ToString(), out object result))
{
return result;
}
}
return value?.ToString();
}
/// <inheritdoc/>
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
if (IsInversed)
{
return value;
}
else if (Enum.TryParse(Type, value?.ToString(), out object result))
{
return result;
}
return value;
}
}
}