62 lines
2.0 KiB
C#
62 lines
2.0 KiB
C#
/*
|
|
* Copyright (c) 2022 LG Electronics Inc.
|
|
* SPDX-License-Identifier: LicenseRef-LGE-Proprietary
|
|
*/
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Converters;
|
|
using System.Windows.Data;
|
|
|
|
namespace AuroraUIComponent.UIConverter
|
|
{
|
|
public class IndexConverter : IValueConverter
|
|
{
|
|
public string Prefix { get; set; } = string.Empty;
|
|
public string Postfix { get; set; } = string.Empty;
|
|
public int Offset { get; set; } = 1;
|
|
public int IndexDigit { get; set; } = -1;
|
|
|
|
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
|
{
|
|
int intVal = -1;
|
|
if (value is int index) {
|
|
intVal = index;
|
|
} else {
|
|
try {
|
|
intVal = (int)value;
|
|
} catch (Exception ex) {
|
|
return value;
|
|
}
|
|
}
|
|
|
|
if (IndexDigit < 0) {
|
|
return Prefix + (intVal + Offset).ToString() + Postfix;
|
|
} else {
|
|
//return Prefix + (index + Offset).ToString($"N{IndexDigit}") + Postfix;
|
|
return Prefix + (intVal + Offset).ToString($"D{IndexDigit}") + Postfix;
|
|
}
|
|
}
|
|
|
|
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
|
{
|
|
if (value is string strValue) {
|
|
if (!string.IsNullOrEmpty(Prefix) && strValue.Contains(Prefix)) {
|
|
strValue = strValue.Replace(Prefix, string.Empty);
|
|
}
|
|
if (!string.IsNullOrEmpty(Postfix) && strValue.Contains(Postfix)) {
|
|
strValue = strValue.Replace(Postfix, string.Empty);
|
|
}
|
|
if (int.TryParse(strValue, out int intValue)) {
|
|
return intValue - Offset;
|
|
}
|
|
}
|
|
|
|
return value;
|
|
}
|
|
}
|
|
}
|