using System; using System.ComponentModel; using System.Globalization; using System.Windows; using System.Windows.Controls; using System.Windows.Input; namespace Muchinfo.DataPager.Base { public class MuchinfoPagerTextBox : TextBox, ICommandSource //, IThemable { public static readonly DependencyProperty PageIndexProperty = DependencyProperty.Register("PageIndex", typeof(int), typeof(MuchinfoPagerTextBox), new PropertyMetadata(-1, new PropertyChangedCallback(MuchinfoPagerTextBox.PageIndexChanged))); public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(MuchinfoPagerTextBox), new PropertyMetadata(null)); public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(MuchinfoPagerTextBox), new PropertyMetadata(new PropertyChangedCallback(MuchinfoPagerTextBox.OnCommandChanged))); public static readonly DependencyProperty CommandTargetProperty = DependencyProperty.Register("CommandTarget", typeof(UIElement), typeof(MuchinfoPagerTextBox), null); public int PageIndex { get { return (int)base.GetValue(MuchinfoPagerTextBox.PageIndexProperty); } set { base.SetValue(MuchinfoPagerTextBox.PageIndexProperty, value); } } [TypeConverter(typeof(CommandConverter))] public ICommand Command { get { return (ICommand)base.GetValue(MuchinfoPagerTextBox.CommandProperty); } set { base.SetValue(MuchinfoPagerTextBox.CommandProperty, value); } } public object CommandParameter { get { return base.GetValue(MuchinfoPagerTextBox.CommandParameterProperty); } set { base.SetValue(MuchinfoPagerTextBox.CommandParameterProperty, value); } } public UIElement CommandTarget { get { return (UIElement)base.GetValue(MuchinfoPagerTextBox.CommandTargetProperty); } set { base.SetValue(MuchinfoPagerTextBox.CommandTargetProperty, value); } } IInputElement ICommandSource.CommandTarget { get { return null; } } private static void PageIndexChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args) { ((MuchinfoPagerTextBox)sender).Text = ((int)args.NewValue + 1).ToString(); } protected override void OnKeyDown(KeyEventArgs e) { base.OnKeyDown(e); if (e.Key == Key.Return) { this.RaiseCommand(); } } private void RaiseCommand() { this.CommandParameter = this.ConvertTextToParameter(); if (this.CommandParameter != null) { this.ExecuteCommand(); } base.Text = (this.PageIndex + 1).ToString(); } internal int ConvertTextToParameter() { int num; if (int.TryParse(base.Text, NumberStyles.Integer, CultureInfo.InvariantCulture, out num)) { return num - 1; } return this.PageIndex; } protected override void OnLostFocus(RoutedEventArgs e) { base.OnLostFocus(e); this.RaiseCommand(); } private void ExecuteCommand() { if (this.Command != null) { var routedCommand = this.Command as RoutedCommand; if (routedCommand == null) { this.Command.Execute(this.CommandParameter); return; } routedCommand.Execute(this.CommandParameter, this.CommandTarget ?? this); } } private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var dataPagerTextBox = d as MuchinfoPagerTextBox; if (dataPagerTextBox != null) { dataPagerTextBox.ChangeCommand((ICommand)e.OldValue, (ICommand)e.NewValue); } } private void ChangeCommand(ICommand oldCommand, ICommand newCommand) { if (oldCommand != null) { oldCommand.CanExecuteChanged -= new EventHandler(this.CanExecuteChanged); } if (newCommand != null) { newCommand.CanExecuteChanged += new EventHandler(this.CanExecuteChanged); } this.CanExecuteApply(); } private void CanExecuteApply() { if (this.Command != null) { var routedCommand = this.Command as RoutedCommand; base.IsEnabled = ((routedCommand == null) ? this.Command.CanExecute(this.CommandParameter) : routedCommand.CanExecute(this.CommandParameter, this.CommandTarget ?? this)); } } private void CanExecuteChanged(object sender, EventArgs e) { this.CanExecuteApply(); } } }