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.

52 lines
2.0 KiB

  1. using System;
  2. using System.ComponentModel;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Windows.Controls;
  6. using System.Windows;
  7. namespace BPA.UIControl.Commons
  8. {
  9. /// <summary>
  10. /// RowDefinitionCollection 类型转换
  11. /// from https://github.com/dotnet/maui
  12. /// Microsoft.Maui.Controls.RowDefinitionCollectionTypeConverter.cs
  13. /// </summary>
  14. public class RowDefinitionCollectionTypeConverter : TypeConverter
  15. {
  16. /// <inheritdoc/>
  17. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  18. => sourceType == typeof(string);
  19. /// <inheritdoc/>
  20. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  21. => true;
  22. /// <inheritdoc/>
  23. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  24. {
  25. var strValue = value?.ToString();
  26. if (strValue != null)
  27. {
  28. var lengths = strValue.Split(',');
  29. var converter = new GridLengthConverter();
  30. var definitions = new RowDefinition[lengths.Length];
  31. for (var i = 0; i < lengths.Length; i++)
  32. definitions[i] = new RowDefinition { Height = (GridLength)converter.ConvertFromInvariantString(lengths[i]) };
  33. return definitions;
  34. }
  35. throw new InvalidOperationException(string.Format("Cannot convert \"{0}\" into {1}", strValue, typeof(RowDefinitionCollection)));
  36. }
  37. /// <inheritdoc/>
  38. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  39. {
  40. if (!(value is RowDefinitionCollection rdc))
  41. throw new NotSupportedException();
  42. var converter = new GridLengthConverter();
  43. return string.Join(", ", rdc.Select(rd => converter.ConvertToInvariantString(rd.Height)));
  44. }
  45. }
  46. }