using MultiTerm.Core.ViewModel; using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Windows.Data; namespace MultiTerm.Wpf.CustomControl.ValueConverter; [ValueConversion(typeof(IEnumerable), typeof(string))] public class DataViewModelToStringConverter : IValueConverter { /// /// Newline Sequence that is introduced whenever a new line begins in the DataViewModel. /// public static readonly char NewlineSequence = '\n'; /// /// Convert from IEnumerable to string /// /// /// /// /// /// /// public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { // guard null if(value is null) { return string.Empty; } // guard type if (value is not IEnumerable enumerable) { throw new ArgumentException($"Can only convert from {nameof(IEnumerable)} value"); } string returnString = string.Empty; // iterate through data, adding content to textbox int prevCounter = enumerable.First().LineIdentifier; for (int i = 0; i < enumerable.Count(); i++) { DataViewModel item = enumerable.ElementAt(i); // newline if previous counter is lower than this if (item.LineIdentifier > prevCounter) { returnString += NewlineSequence; prevCounter = item.LineIdentifier; } // add character (text) returnString += item.DisplayStringUtf16; } return returnString; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }