96 lines
3.0 KiB
C#
96 lines
3.0 KiB
C#
using System;
|
|
using System.Globalization;
|
|
using System.Windows;
|
|
using System.Windows.Data;
|
|
|
|
namespace CtrEditor.Converters
|
|
{
|
|
/// <summary>
|
|
/// Converter para calcular el margen de las secciones del tanque
|
|
/// basado en los porcentajes de las secciones previas
|
|
/// </summary>
|
|
public class TankSectionMarginConverter : IMultiValueConverter
|
|
{
|
|
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
|
|
{
|
|
if (values == null || values.Length < 2)
|
|
return new Thickness(4, 4, 4, 4);
|
|
|
|
// Convertir valores a double
|
|
if (!TryConvertToDouble(values[values.Length - 1], out double tankSize))
|
|
return new Thickness(4, 4, 4, 4);
|
|
|
|
// Calcular la altura total acumulada de las secciones previas
|
|
double accumulatedPercentage = 0.0;
|
|
for (int i = 0; i < values.Length - 1; i++) // -1 para excluir tankSize
|
|
{
|
|
if (TryConvertToDouble(values[i], out double percentage))
|
|
{
|
|
accumulatedPercentage += percentage;
|
|
}
|
|
}
|
|
|
|
try
|
|
{
|
|
// Convertir tankSize a píxeles (100 píxeles por metro aproximadamente)
|
|
var tankHeightPixels = tankSize * 100.0;
|
|
|
|
// Calcular altura acumulada en píxeles
|
|
var accumulatedHeightPixels = (accumulatedPercentage / 100.0) * tankHeightPixels;
|
|
|
|
// Calcular margen bottom basado en la altura acumulada
|
|
var marginBottom = 4.0 + accumulatedHeightPixels;
|
|
|
|
return new Thickness(4, 4, 4, marginBottom);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return new Thickness(4, 4, 4, 4);
|
|
}
|
|
}
|
|
|
|
private bool TryConvertToDouble(object value, out double result)
|
|
{
|
|
result = 0.0;
|
|
|
|
if (value == null)
|
|
return false;
|
|
|
|
// Intentar conversión directa si ya es double
|
|
if (value is double d)
|
|
{
|
|
result = d;
|
|
return true;
|
|
}
|
|
|
|
// Intentar conversión desde float
|
|
if (value is float f)
|
|
{
|
|
result = f;
|
|
return true;
|
|
}
|
|
|
|
// Intentar conversión desde int
|
|
if (value is int i)
|
|
{
|
|
result = i;
|
|
return true;
|
|
}
|
|
|
|
// Intentar parsing desde string
|
|
if (value is string s)
|
|
{
|
|
if (double.TryParse(s.Replace(',', '.'), NumberStyles.Float, CultureInfo.InvariantCulture, out result))
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
|
|
{
|
|
throw new NotImplementedException("ConvertBack no está implementado para TankSectionMarginConverter");
|
|
}
|
|
}
|
|
}
|