You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
2.1 KiB
65 lines
2.1 KiB
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<DataViewModel>), typeof(string))]
|
|
public class DataViewModelToStringConverter : IValueConverter
|
|
{
|
|
/// <summary>
|
|
/// Newline Sequence that is introduced whenever a new line begins in the DataViewModel.
|
|
/// </summary>
|
|
public static readonly char NewlineSequence = '\n';
|
|
|
|
/// <summary>
|
|
/// Convert from IEnumerable<DataViewModel> to string
|
|
/// </summary>
|
|
/// <param name="value"></param>
|
|
/// <param name="targetType"></param>
|
|
/// <param name="parameter"></param>
|
|
/// <param name="culture"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
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<DataViewModel> enumerable) { throw new ArgumentException($"Can only convert from {nameof(IEnumerable<DataViewModel>)} value"); }
|
|
|
|
// guard empty enumerable
|
|
if (enumerable.Count() == 0) { return string.Empty; }
|
|
|
|
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();
|
|
}
|
|
}
|
|
|