TextBoxExtensions.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Data.Converters;
  4. using Avalonia.Media;
  5. using Avalonia.Threading;
  6. using System;
  7. using System.Globalization;
  8. namespace SukiUI.Theme;
  9. public static class TextBoxExtensions
  10. {
  11. public static readonly AttachedProperty<string> PrefixProperty =
  12. AvaloniaProperty.RegisterAttached<TextBox, string>("Prefix", typeof(TextBox), defaultValue: "");
  13. public static string GetPrefix(TextBox textBox)
  14. {
  15. return textBox.GetValue(PrefixProperty);
  16. }
  17. public static void SetPrefix(TextBox textBox, string value)
  18. {
  19. textBox.SetValue(PrefixProperty, value);
  20. }
  21. public static readonly AttachedProperty<bool> AddDeleteButtonProperty =
  22. AvaloniaProperty.RegisterAttached<TextBox, bool>("AddDeleteButton", typeof(TextBox), defaultValue: false);
  23. public static bool GetAddDeleteButton(TextBox textBox)
  24. {
  25. return textBox.GetValue(AddDeleteButtonProperty);
  26. }
  27. public static void SetAddDeleteButton(TextBox textBox, bool value)
  28. {
  29. textBox.SetValue(AddDeleteButtonProperty, value);
  30. }
  31. public static void Error(this TextBox textbox, string message)
  32. {
  33. Dispatcher.UIThread.Invoke(() =>
  34. {
  35. textbox.BorderBrush = Brushes.IndianRed;
  36. textbox.ContextFlyout = new Flyout()
  37. {
  38. Placement = PlacementMode.BottomEdgeAlignedLeft,
  39. Content = new TextBlock() { Text = message, FontWeight = FontWeight.Thin, Foreground = Brushes.IndianRed }
  40. };
  41. textbox.ContextFlyout.ShowAt(textbox);
  42. textbox.Vibrate(TimeSpan.FromMilliseconds(400));
  43. });
  44. }
  45. }
  46. public static class ButtonExtensions
  47. {
  48. public static readonly AttachedProperty<bool> ShowProgressProperty =
  49. AvaloniaProperty.RegisterAttached<Button, bool>("ShowProgress", typeof(Button), defaultValue: false);
  50. public static bool GetShowProgress(Button textBox)
  51. {
  52. return textBox.GetValue(ShowProgressProperty);
  53. }
  54. public static void SetShowProgress(Button textBox, bool value)
  55. {
  56. textBox.SetValue(ShowProgressProperty, value);
  57. }
  58. public static void ShowProgress(this Button textBox)
  59. {
  60. textBox.SetValue(ShowProgressProperty, true);
  61. }
  62. public static void HideProgress(this Button textBox)
  63. {
  64. textBox.SetValue(ShowProgressProperty, false);
  65. }
  66. }
  67. public class StringToDoubleConverter : IValueConverter
  68. {
  69. public static readonly StringToDoubleConverter Instance = new();
  70. public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
  71. {
  72. if (value == null)
  73. return 0;
  74. return ((string)value).Length > 0 ? 1 : 0;
  75. }
  76. public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
  77. {
  78. throw new NotSupportedException();
  79. }
  80. }
  81. public class BoolToWidthProgressConverter : IValueConverter
  82. {
  83. public static readonly BoolToWidthProgressConverter Instance = new();
  84. public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
  85. {
  86. if (value == null)
  87. return 0;
  88. return ((bool)value) ? 40 : 0;
  89. }
  90. public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
  91. {
  92. throw new NotSupportedException();
  93. }
  94. }