You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Common.cs 4.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System.ComponentModel;
  2. using System;
  3. using System.Windows.Input;
  4. using System.Globalization;
  5. using System.Windows.Data;
  6. using System.Windows.Controls;
  7. using System.Windows;
  8. namespace starter.viewmodel.common
  9. {
  10. public abstract class NotificationObject : INotifyPropertyChanged
  11. {
  12. public event PropertyChangedEventHandler? PropertyChanged;
  13. ///< summary>
  14. /// announce notification
  15. /// </summary>
  16. ///< param name="propertyName">property name</param>
  17. public void RaisePropertyChanged(string propertyName)
  18. {
  19. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  20. }
  21. }
  22. ///< summary>
  23. /// BaseCommand
  24. /// </summary>
  25. public class BaseCommand : ICommand
  26. {
  27. private Func<object?, bool>? _canExecute;
  28. private Action<object?> _execute;
  29. public BaseCommand(Func<object?, bool>? canExecute, Action<object?> execute)
  30. {
  31. _canExecute = canExecute;
  32. _execute = execute;
  33. }
  34. public BaseCommand(Action<object?> execute) :
  35. this(null, execute)
  36. {
  37. }
  38. public event EventHandler? CanExecuteChanged
  39. {
  40. add
  41. {
  42. if (_canExecute != null)
  43. {
  44. CommandManager.RequerySuggested += value;
  45. }
  46. }
  47. remove
  48. {
  49. if (_canExecute != null)
  50. {
  51. CommandManager.RequerySuggested -= value;
  52. }
  53. }
  54. }
  55. public bool CanExecute(object? parameter)
  56. {
  57. return _canExecute == null ? true : _canExecute(parameter);
  58. }
  59. public void Execute(object? parameter)
  60. {
  61. if (_execute != null && CanExecute(parameter))
  62. {
  63. _execute(parameter);
  64. }
  65. }
  66. }
  67. public class RadioConverter : IValueConverter
  68. {
  69. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  70. {
  71. if (value == null || parameter == null)
  72. {
  73. return false;
  74. }
  75. string checkvalue = value.ToString() ?? "";
  76. string targetvalue = parameter.ToString() ?? "";
  77. bool r = checkvalue.Equals(targetvalue);
  78. return r;
  79. }
  80. public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
  81. {
  82. if (value is null || parameter is null)
  83. {
  84. return null;
  85. }
  86. if ((bool)value)
  87. {
  88. return parameter.ToString();
  89. }
  90. return null;
  91. }
  92. }
  93. /// <summary>
  94. /// Password 附加属性,来自https://blog.csdn.net/qq_43562262/article/details/121786337
  95. /// </summary>
  96. public class PasswordHelper
  97. {
  98. public static readonly DependencyProperty PasswordProperty = DependencyProperty.RegisterAttached("Password", typeof(string), typeof(PasswordHelper),
  99. new PropertyMetadata(new PropertyChangedCallback(OnPropertyChanged)));
  100. public static string GetPassword(DependencyObject d)
  101. {
  102. return (string)d.GetValue(PasswordProperty);
  103. }
  104. public static void SetPassword(DependencyObject d, string value)
  105. {
  106. d.SetValue(PasswordProperty, value);
  107. }
  108. public static readonly DependencyProperty AttachProperty = DependencyProperty.RegisterAttached("Attach", typeof(string), typeof(PasswordHelper),
  109. new PropertyMetadata(new PropertyChangedCallback(OnAttachChanged)));
  110. public static string GetAttach(DependencyObject d)
  111. {
  112. return (string)d.GetValue(AttachProperty);
  113. }
  114. public static void SetAttach(DependencyObject d, string value)
  115. {
  116. d.SetValue(AttachProperty, value);
  117. }
  118. static bool _isUpdating = false;
  119. private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  120. {
  121. PasswordBox pb = (d as PasswordBox)!;
  122. pb.PasswordChanged -= Pb_PasswordChanged;
  123. if (!_isUpdating)
  124. (d as PasswordBox)!.Password = e.NewValue.ToString();
  125. pb.PasswordChanged += Pb_PasswordChanged;
  126. }
  127. private static void OnAttachChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  128. {
  129. PasswordBox pb = (d as PasswordBox)!;
  130. pb.PasswordChanged += Pb_PasswordChanged;
  131. }
  132. private static void Pb_PasswordChanged(object sender, RoutedEventArgs e)
  133. {
  134. PasswordBox pb = (sender as PasswordBox)!;
  135. _isUpdating = true;
  136. SetPassword(pb, pb.Password);
  137. _isUpdating = false;
  138. }
  139. }
  140. }