using Muchinfo.DataPager.Extensions; using System.Windows; using System.Windows.Controls; using System.Windows.Input; namespace Muchinfo.DataPager.Base { public class MuchinfoNumericElementsPresenter : Control { public static readonly DependencyProperty NumericButtonStyleProperty; public static readonly DependencyProperty NumericButtonCountProperty; public static readonly DependencyProperty PageIndexProperty; public static readonly DependencyProperty PageCountProperty; private MuchinfoNumericElementsViewModel _viewModel; public MuchinfoNumericElementsViewModel ViewModel { get { return this._viewModel ?? (this._viewModel = new MuchinfoNumericElementsViewModel(this.NumericButtonCount, this.PageIndex, this.PageCount, this.NumericButtonStyle)); } } public Style NumericButtonStyle { get { return (Style)base.GetValue(MuchinfoNumericElementsPresenter.NumericButtonStyleProperty); } set { base.SetValue(MuchinfoNumericElementsPresenter.NumericButtonStyleProperty, value); } } public int NumericButtonCount { get { return (int)base.GetValue(MuchinfoNumericElementsPresenter.NumericButtonCountProperty); } set { base.SetValue(MuchinfoNumericElementsPresenter.NumericButtonCountProperty, value); } } public int PageIndex { get { return (int)base.GetValue(MuchinfoNumericElementsPresenter.PageIndexProperty); } set { base.SetValue(MuchinfoNumericElementsPresenter.PageIndexProperty, value); } } public int PageCount { get { return (int)base.GetValue(MuchinfoNumericElementsPresenter.PageCountProperty); } set { base.SetValue(MuchinfoNumericElementsPresenter.PageCountProperty, value); } } public MuchinfoNumericElementsPresenter() { base.DataContext = this.ViewModel; base.AddHandler(UIElement.MouseLeftButtonUpEvent, new MouseButtonEventHandler(MuchinfoNumericElementsPresenter.OnButtonPressed), true); } private static void OnButtonPressed(object sender, MouseButtonEventArgs args) { var presenter = sender as MuchinfoNumericElementsPresenter; var radioButton = ((UIElement)args.OriginalSource).ParentOfType(); if (presenter != null && radioButton != null) { MuchinfoNumericElementsPresenter.SyncRadioButtonStateWithPagerState(presenter, radioButton); } } private static void SyncRadioButtonStateWithPagerState(MuchinfoNumericElementsPresenter pagesPresenter, RadioButton radioButton) { if (!((MuchinfoNumericElementViewModel)radioButton.DataContext).IsCurrent) { MuchinfoNumericElementsPresenter.PopulatePages(pagesPresenter); } } static MuchinfoNumericElementsPresenter() { MuchinfoNumericElementsPresenter.NumericButtonStyleProperty = DependencyProperty.Register("NumericButtonStyle", typeof(Style), typeof(MuchinfoNumericElementsPresenter), new PropertyMetadata(null)); MuchinfoNumericElementsPresenter.NumericButtonCountProperty = DependencyProperty.Register("NumericButtonCount", typeof(int), typeof(MuchinfoNumericElementsPresenter), new PropertyMetadata(new PropertyChangedCallback(MuchinfoNumericElementsPresenter.PagesRelatedDependencyPropertyChanged))); MuchinfoNumericElementsPresenter.PageIndexProperty = DependencyProperty.Register("PageIndex", typeof(int), typeof(MuchinfoNumericElementsPresenter), new PropertyMetadata(new PropertyChangedCallback(MuchinfoNumericElementsPresenter.PagesRelatedDependencyPropertyChanged))); MuchinfoNumericElementsPresenter.PageCountProperty = DependencyProperty.Register("PageCount", typeof(int), typeof(MuchinfoNumericElementsPresenter), new PropertyMetadata(new PropertyChangedCallback(MuchinfoNumericElementsPresenter.PagesRelatedDependencyPropertyChanged))); FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(MuchinfoNumericElementsPresenter), new FrameworkPropertyMetadata(typeof(MuchinfoNumericElementsPresenter))); } private static void PagesRelatedDependencyPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args) { var pagesPresenter = (MuchinfoNumericElementsPresenter)sender; MuchinfoNumericElementsPresenter.PopulatePages(pagesPresenter); } private static void PopulatePages(MuchinfoNumericElementsPresenter pagesPresenter) { pagesPresenter.ViewModel.PopulatePages(pagesPresenter.NumericButtonCount, pagesPresenter.PageIndex, pagesPresenter.PageCount, pagesPresenter.NumericButtonStyle); } } }