MuchinfoNumericElementsPresenter.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using Muchinfo.DataPager.Extensions;
  2. using System.Windows;
  3. using System.Windows.Controls;
  4. using System.Windows.Input;
  5. namespace Muchinfo.DataPager.Base
  6. {
  7. public class MuchinfoNumericElementsPresenter : Control
  8. {
  9. public static readonly DependencyProperty NumericButtonStyleProperty;
  10. public static readonly DependencyProperty NumericButtonCountProperty;
  11. public static readonly DependencyProperty PageIndexProperty;
  12. public static readonly DependencyProperty PageCountProperty;
  13. private MuchinfoNumericElementsViewModel _viewModel;
  14. public MuchinfoNumericElementsViewModel ViewModel
  15. {
  16. get
  17. {
  18. return this._viewModel ?? (this._viewModel = new MuchinfoNumericElementsViewModel(this.NumericButtonCount, this.PageIndex, this.PageCount, this.NumericButtonStyle));
  19. }
  20. }
  21. public Style NumericButtonStyle
  22. {
  23. get
  24. {
  25. return (Style)base.GetValue(MuchinfoNumericElementsPresenter.NumericButtonStyleProperty);
  26. }
  27. set
  28. {
  29. base.SetValue(MuchinfoNumericElementsPresenter.NumericButtonStyleProperty, value);
  30. }
  31. }
  32. public int NumericButtonCount
  33. {
  34. get
  35. {
  36. return (int)base.GetValue(MuchinfoNumericElementsPresenter.NumericButtonCountProperty);
  37. }
  38. set
  39. {
  40. base.SetValue(MuchinfoNumericElementsPresenter.NumericButtonCountProperty, value);
  41. }
  42. }
  43. public int PageIndex
  44. {
  45. get
  46. {
  47. return (int)base.GetValue(MuchinfoNumericElementsPresenter.PageIndexProperty);
  48. }
  49. set
  50. {
  51. base.SetValue(MuchinfoNumericElementsPresenter.PageIndexProperty, value);
  52. }
  53. }
  54. public int PageCount
  55. {
  56. get
  57. {
  58. return (int)base.GetValue(MuchinfoNumericElementsPresenter.PageCountProperty);
  59. }
  60. set
  61. {
  62. base.SetValue(MuchinfoNumericElementsPresenter.PageCountProperty, value);
  63. }
  64. }
  65. public MuchinfoNumericElementsPresenter()
  66. {
  67. base.DataContext = this.ViewModel;
  68. base.AddHandler(UIElement.MouseLeftButtonUpEvent, new MouseButtonEventHandler(MuchinfoNumericElementsPresenter.OnButtonPressed), true);
  69. }
  70. private static void OnButtonPressed(object sender, MouseButtonEventArgs args)
  71. {
  72. var presenter = sender as MuchinfoNumericElementsPresenter;
  73. var radioButton = ((UIElement)args.OriginalSource).ParentOfType<RadioButton>();
  74. if (presenter != null && radioButton != null)
  75. {
  76. MuchinfoNumericElementsPresenter.SyncRadioButtonStateWithPagerState(presenter, radioButton);
  77. }
  78. }
  79. private static void SyncRadioButtonStateWithPagerState(MuchinfoNumericElementsPresenter pagesPresenter, RadioButton radioButton)
  80. {
  81. if (!((MuchinfoNumericElementViewModel)radioButton.DataContext).IsCurrent)
  82. {
  83. MuchinfoNumericElementsPresenter.PopulatePages(pagesPresenter);
  84. }
  85. }
  86. static MuchinfoNumericElementsPresenter()
  87. {
  88. MuchinfoNumericElementsPresenter.NumericButtonStyleProperty = DependencyProperty.Register("NumericButtonStyle", typeof(Style), typeof(MuchinfoNumericElementsPresenter), new PropertyMetadata(null));
  89. MuchinfoNumericElementsPresenter.NumericButtonCountProperty = DependencyProperty.Register("NumericButtonCount", typeof(int), typeof(MuchinfoNumericElementsPresenter), new PropertyMetadata(new PropertyChangedCallback(MuchinfoNumericElementsPresenter.PagesRelatedDependencyPropertyChanged)));
  90. MuchinfoNumericElementsPresenter.PageIndexProperty = DependencyProperty.Register("PageIndex", typeof(int), typeof(MuchinfoNumericElementsPresenter), new PropertyMetadata(new PropertyChangedCallback(MuchinfoNumericElementsPresenter.PagesRelatedDependencyPropertyChanged)));
  91. MuchinfoNumericElementsPresenter.PageCountProperty = DependencyProperty.Register("PageCount", typeof(int), typeof(MuchinfoNumericElementsPresenter), new PropertyMetadata(new PropertyChangedCallback(MuchinfoNumericElementsPresenter.PagesRelatedDependencyPropertyChanged)));
  92. FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(MuchinfoNumericElementsPresenter), new FrameworkPropertyMetadata(typeof(MuchinfoNumericElementsPresenter)));
  93. }
  94. private static void PagesRelatedDependencyPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
  95. {
  96. var pagesPresenter = (MuchinfoNumericElementsPresenter)sender;
  97. MuchinfoNumericElementsPresenter.PopulatePages(pagesPresenter);
  98. }
  99. private static void PopulatePages(MuchinfoNumericElementsPresenter pagesPresenter)
  100. {
  101. pagesPresenter.ViewModel.PopulatePages(pagesPresenter.NumericButtonCount, pagesPresenter.PageIndex, pagesPresenter.PageCount, pagesPresenter.NumericButtonStyle);
  102. }
  103. }
  104. }