public

ValueConverter to convert an IEnumerable of anything into a string with custom separator

  • Download Gist
EnumerableToTextConverter.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
using System;
using System.Collections;
using System.Linq;
using System.Windows.Data;
 
namespace LedgerscopeApplication.ValueConverters
{
public class EnumerableToTextConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (parameter == null)
parameter = Environment.NewLine;
var val = value as IEnumerable;
if (val != null)
{
var retVal = string.Join(parameter.ToString(), val.OfType<object>().Select(a => a.ToString()));
return retVal;
}
else
{
return string.Empty;
}
}
 
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.