HomePageButton.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Runtime.CompilerServices;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. namespace ComPDFKit.Controls.Common
  11. {
  12. public class HomePageButton : Button
  13. {
  14. public static readonly DependencyProperty IsToggledProperty =
  15. DependencyProperty.Register("IsToggled", typeof(bool), typeof(HomePageButton), new PropertyMetadata(false));
  16. public bool IsToggled
  17. {
  18. get { return (bool)GetValue(IsToggledProperty); }
  19. set { SetValue(IsToggledProperty, value); }
  20. }
  21. public static readonly RoutedEvent ToggledEvent =
  22. EventManager.RegisterRoutedEvent("Toggled", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(HomePageButton));
  23. public event RoutedEventHandler Toggled
  24. {
  25. add { AddHandler(ToggledEvent, value); }
  26. remove { RemoveHandler(ToggledEvent, value); }
  27. }
  28. public HomePageButton()
  29. {
  30. Click += HomePageButton_Click; ;
  31. }
  32. public class QueryLockEventArgs : EventArgs
  33. {
  34. public bool CanLock { get; set; }
  35. }
  36. public delegate bool QueryLockHandler(object sender, QueryLockEventArgs e);
  37. public event QueryLockHandler QueryLock;
  38. private void HomePageButton_Click(object sender, RoutedEventArgs e)
  39. {
  40. if (QueryLock?.Invoke(this, new QueryLockEventArgs()) == false)
  41. {
  42. IsToggled = !IsToggled;
  43. RaiseToggledEvent();
  44. }
  45. }
  46. private void RaiseToggledEvent()
  47. {
  48. RoutedEventArgs newEventArgs = new RoutedEventArgs(ToggledEvent);
  49. RaiseEvent(newEventArgs);
  50. }
  51. }
  52. }