git_bsmd/ENI2/Util/ExpiryStateConverter.cs

44 lines
1.1 KiB
C#

using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Markup;
namespace ENI2.Util
{
public class ExpiryStateConverter : MarkupExtension, IValueConverter
{
private static ExpiryStateConverter _converter;
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (_converter == null)
{
_converter = new ExpiryStateConverter();
}
return _converter;
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (!(value is DateTime dt))
return "Unknown";
DateTime today = DateTime.Today;
DateTime validUntil = dt.Date;
if (validUntil < today)
return "Expired";
if (validUntil <= today.AddMonths(1))
return "ExpiringSoon";
return "Ok";
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}