wabbajack/Wabbajack/Converters/AbsolutePathToStringConverter.cs

80 lines
2.3 KiB
C#
Raw Normal View History

2020-03-31 22:05:36 +00:00
using System;
using System.Globalization;
using System.Windows.Data;
2020-03-31 22:05:36 +00:00
using ReactiveUI;
using Wabbajack.Common;
namespace Wabbajack
2020-03-31 22:05:36 +00:00
{
public class AbsolutePathToStringConverter : IBindingTypeConverter, IValueConverter
2020-03-31 22:05:36 +00:00
{
public int GetAffinityForObjects(Type fromType, Type toType)
{
if (toType == typeof(object)) return 1;
if (toType == typeof(string)) return 1;
if (toType == typeof(AbsolutePath)) return 1;
if (toType == typeof(AbsolutePath?)) return 1;
return 0;
}
public bool TryConvert(object @from, Type toType, object conversionHint, out object result)
{
if (toType == typeof(AbsolutePath))
{
if (@from is string s)
{
try
{
result = (AbsolutePath)s;
return true;
}
catch
{
result = (AbsolutePath)"";
return false;
}
}
if (@from is AbsolutePath abs)
{
result = abs;
return true;
}
}
else if (toType == typeof(string))
{
if (@from is string s)
{
result = default;
return false;
}
if (@from is AbsolutePath abs)
{
result = (string)abs;
return true;
}
}
result = default;
return false;
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (targetType != typeof(string))
throw new InvalidOperationException($"The target must be of type string");
if (value is AbsolutePath path)
{
return path.ToString();
}
return string.Empty;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return (AbsolutePath)(value as string);
}
2020-03-31 22:05:36 +00:00
}
}