Files

51 lines
1.8 KiB
C#
Raw Permalink Normal View History

2026-08-10 14:45:47 +02:00
using System;
using System.Globalization;
using Avalonia.Data.Converters;
namespace Avalonia.PropertyGrid.Controls;
/// <summary>
/// Converts PropertyType to appropriate numeric format string
/// </summary>
public sealed class NumericFormatConverter : IValueConverter
2026-08-10 14:45:47 +02:00
{
/// <summary>
/// Converts a Type to an appropriate numeric format string.
/// </summary>
/// <param name="value">The Type to convert.</param>
/// <param name="targetType">The target type.</param>
/// <param name="parameter">Optional parameter.</param>
/// <param name="culture">Culture information.</param>
/// <returns>A format string like "N2" for floating point or "N0" for integers.</returns>
2026-08-10 14:45:47 +02:00
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is Type type)
{
// Floating point types use decimal places
if (type == typeof(float) || type == typeof(double) || type == typeof(decimal))
{
return "N2"; // 2 decimal places
}
// Integer types use no decimal places
return "N0";
}
return "N0";
}
/// <summary>
/// Not supported for this converter.
/// </summary>
/// <param name="value">The value to convert back.</param>
/// <param name="targetType">The target type.</param>
/// <param name="parameter">Optional parameter.</param>
/// <param name="culture">Culture information.</param>
/// <returns>Throws NotImplementedException.</returns>
/// <exception cref="NotImplementedException">This converter does not support ConvertBack.</exception>
2026-08-10 14:45:47 +02:00
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}