EnumCaseStringConverter - cleanup code style/syntax
This commit is contained in:
1 parent
762369ad2b
commit
a29eaa2618
1 file changed
+7
-20
@@ -1,28 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Data;
|
||||
|
||||
namespace mbts.ui.converters
|
||||
{
|
||||
public class EnumCaseStringConverter<T> : IValueConverter where T : struct
|
||||
{
|
||||
namespace mbts.ui.converters {
|
||||
class EnumCaseStringConverter<T> : IValueConverter where T : struct {
|
||||
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
|
||||
T t = default(T);
|
||||
if (value is T)
|
||||
t = (T)value;
|
||||
string raw_str = t.ToString();
|
||||
return raw_str.Substring(0, 1) + new String(raw_str.Skip(1).Select(Char.ToLower).ToLArray());
|
||||
string raw_str = (value is T t ? t : default).ToString();
|
||||
return raw_str.Substring(0, 1) + new string(raw_str.Skip(1).Select(char.ToLower).ToArray());
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
|
||||
T t = default(T);
|
||||
string str = value as string;
|
||||
if (str != null)
|
||||
Enum.TryParse<T>(str, true, out t);
|
||||
return t;
|
||||
}
|
||||
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) =>
|
||||
value is string str && Enum.TryParse(str, true, out T result) ? result : default;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user