37 lines
1.2 KiB
C#
37 lines
1.2 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.ObjectModel;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Windows.Data;
|
|
|
|
namespace DesktopBot.Converters
|
|
{
|
|
/// <summary>
|
|
/// Converte una IEnumerable in una collezione limitata ai primi N elementi.
|
|
/// Parametro: numero massimo di elementi da visualizzare (default 50).
|
|
/// </summary>
|
|
public class MaxItemsConverter : IValueConverter
|
|
{
|
|
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
|
{
|
|
if (!(value is IEnumerable enumerable))
|
|
return value;
|
|
|
|
// Estrai il parametro per il limite
|
|
int maxItems = 50;
|
|
if (parameter != null && int.TryParse(parameter.ToString(), out int parsedMax))
|
|
maxItems = parsedMax;
|
|
|
|
// Converti a lista e prendi gli ultimi N elementi (ordine LIFO)
|
|
var list = enumerable.Cast<object>().Take(maxItems).ToList();
|
|
return new ObservableCollection<object>(list);
|
|
}
|
|
|
|
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
}
|
|
}
|