ToolSizeToIntConverter.cs 947 B

12345678910111213141516171819202122232425262728293031323334
  1. using System;
  2. using System.Globalization;
  3. using System.Text.RegularExpressions;
  4. using System.Windows.Data;
  5. namespace PixiEditor.Helpers.Converters
  6. {
  7. [ValueConversion(typeof(string), typeof(int))]
  8. internal class ToolSizeToIntConverter
  9. : SingleInstanceConverter<ToolSizeToIntConverter>
  10. {
  11. public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  12. {
  13. return value.ToString();
  14. }
  15. public override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  16. {
  17. if (value is not string s)
  18. {
  19. return null;
  20. }
  21. Match match = Regex.Match(s, @"\d+");
  22. if (!match.Success)
  23. {
  24. return null;
  25. }
  26. return int.Parse(match.Groups[0].ValueSpan);
  27. }
  28. }
  29. }