41 lines
1.3 KiB
C#
41 lines
1.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Data;
|
|
|
|
namespace UIShare.Converters
|
|
{
|
|
public class BoolArrayConverter : IValueConverter
|
|
{
|
|
// bool[] -> string
|
|
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
|
{
|
|
if (value is bool[] arr)
|
|
return "[" + string.Join(",", arr.Select(b => b.ToString().ToLower())) + "]";
|
|
return "";
|
|
}
|
|
|
|
// string -> bool[]
|
|
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
|
{
|
|
if (value is string s)
|
|
{
|
|
s = s.Trim('[', ']', ' ');
|
|
if (string.IsNullOrWhiteSpace(s)) return Array.Empty<bool>();
|
|
var parts = s.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
|
return parts.Select(p =>
|
|
{
|
|
if (bool.TryParse(p, out var b)) return b;
|
|
if (p == "1") return true;
|
|
if (p == "0") return false;
|
|
return false;
|
|
}).ToArray();
|
|
}
|
|
return Array.Empty<bool>();
|
|
}
|
|
}
|
|
}
|