git_bsmd/ENI2/Util/ByteConverter.cs

66 lines
1.7 KiB
C#

// Copyright (c) 2017 schick Informatik
// Description: Helper class to convert database byte values to combobox Selected INdex int's
//
using System;
using System.Windows.Data;
using System.Windows.Markup;
namespace ENI2.Util
{
[ValueConversion(typeof(byte?), typeof(int))]
public class ByteConverter : MarkupExtension, IValueConverter
{
#region MarkupExtension implementation
private static ByteConverter _converter = null;
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (_converter == null)
{
_converter = new ByteConverter();
}
return _converter;
}
#endregion
#region IValueConverter implementation
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
int result = -1;
if (value is byte?)
{
if (((byte?)value).HasValue)
{
byte byteVal = (byte)value;
result = (int)byteVal;
}
}
return result;
}
public object ConvertBack(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
byte? result = null;
if(value is int)
{
if ((int)value >= 0)
{
int intValue = (int)value;
result = (byte)intValue;
}
}
return result;
}
#endregion
}
}