|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- using System.ComponentModel;
- using System;
- using System.Windows.Input;
- using System.Globalization;
- using System.Windows.Data;
- using System.Windows.Controls;
- using System.Windows;
-
- namespace starter.viewmodel.common
- {
- public abstract class NotificationObject : INotifyPropertyChanged
- {
- public event PropertyChangedEventHandler? PropertyChanged;
- ///< summary>
- /// announce notification
- /// </summary>
- ///< param name="propertyName">property name</param>
- public void RaisePropertyChanged(string propertyName)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- ///< summary>
- /// BaseCommand
- /// </summary>
- public class BaseCommand : ICommand
- {
- private Func<object?, bool>? _canExecute;
- private Action<object?> _execute;
-
- public BaseCommand(Func<object?, bool>? canExecute, Action<object?> execute)
- {
- _canExecute = canExecute;
- _execute = execute;
- }
-
- public BaseCommand(Action<object?> execute) :
- this(null, execute)
- {
- }
-
- public event EventHandler? CanExecuteChanged
- {
- add
- {
- if (_canExecute != null)
- {
- CommandManager.RequerySuggested += value;
- }
- }
- remove
- {
- if (_canExecute != null)
- {
- CommandManager.RequerySuggested -= value;
- }
- }
- }
-
- public bool CanExecute(object? parameter)
- {
- return _canExecute == null ? true : _canExecute(parameter);
- }
-
- public void Execute(object? parameter)
- {
- if (_execute != null && CanExecute(parameter))
- {
- _execute(parameter);
- }
- }
- }
-
- public class RadioConverter : IValueConverter
- {
- public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
- {
- if (value == null || parameter == null)
- {
- return false;
- }
- string checkvalue = value.ToString() ?? "";
- string targetvalue = parameter.ToString() ?? "";
- bool r = checkvalue.Equals(targetvalue);
- return r;
- }
-
- public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
- {
- if (value is null || parameter is null)
- {
- return null;
- }
-
- if ((bool)value)
- {
- return parameter.ToString();
- }
- return null;
- }
- }
-
- /// <summary>
- /// Password 附加属性,来自https://blog.csdn.net/qq_43562262/article/details/121786337
- /// </summary>
- public class PasswordHelper
- {
- public static readonly DependencyProperty PasswordProperty = DependencyProperty.RegisterAttached("Password", typeof(string), typeof(PasswordHelper),
- new PropertyMetadata(new PropertyChangedCallback(OnPropertyChanged)));
-
- public static string GetPassword(DependencyObject d)
- {
- return (string)d.GetValue(PasswordProperty);
- }
- public static void SetPassword(DependencyObject d, string value)
- {
- d.SetValue(PasswordProperty, value);
- }
-
- public static readonly DependencyProperty AttachProperty = DependencyProperty.RegisterAttached("Attach", typeof(string), typeof(PasswordHelper),
- new PropertyMetadata(new PropertyChangedCallback(OnAttachChanged)));
-
- public static string GetAttach(DependencyObject d)
- {
- return (string)d.GetValue(AttachProperty);
- }
- public static void SetAttach(DependencyObject d, string value)
- {
- d.SetValue(AttachProperty, value);
- }
-
- static bool _isUpdating = false;
- private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- PasswordBox pb = (d as PasswordBox)!;
- pb.PasswordChanged -= Pb_PasswordChanged;
- if (!_isUpdating)
- (d as PasswordBox)!.Password = e.NewValue.ToString();
- pb.PasswordChanged += Pb_PasswordChanged;
- }
-
- private static void OnAttachChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- PasswordBox pb = (d as PasswordBox)!;
- pb.PasswordChanged += Pb_PasswordChanged;
- }
-
- private static void Pb_PasswordChanged(object sender, RoutedEventArgs e)
- {
- PasswordBox pb = (sender as PasswordBox)!;
- _isUpdating = true;
- SetPassword(pb, pb.Password);
- _isUpdating = false;
- }
- }
-
- }
|