-
Notifications
You must be signed in to change notification settings - Fork 807
Expand file tree
/
Copy pathEmptyToIntMaxValueConverter.cs
More file actions
34 lines (30 loc) · 1.04 KB
/
EmptyToIntMaxValueConverter.cs
File metadata and controls
34 lines (30 loc) · 1.04 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
using System;
using System.Globalization;
using System.Windows.Data;
namespace NETworkManager.Converters;
public class EmptyToIntMaxValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
const int fallback = int.MaxValue;
if (targetType == typeof(int))
{
if (value is not string strValue)
return fallback;
if (string.IsNullOrWhiteSpace(strValue))
return fallback;
if (!int.TryParse(strValue, out int parsedIntValue))
return fallback;
return parsedIntValue;
}
if (targetType != typeof(string))
return null;
if (value is not int intValue)
return null;
return intValue is fallback ? string.Empty : intValue.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return Convert(value, targetType, parameter, culture);
}
}