From bde9f6e5658fdfdca1d11c4668d16e9f4a86724f Mon Sep 17 00:00:00 2001 From: John Colvin Date: Tue, 9 Apr 2019 13:48:47 -0500 Subject: [PATCH] made TickUpDownNullableUInt adopt the textbox behavior/implementation logic of TickUpDownDouble --- .../view/Controls/TickUpDown/TickUpDown.xaml | 3 +- .../TickUpDown/TickUpDownNullableUInt.cs | 95 ++++++++++++++----- .../view/Extensions/UIElementExtensions.cs | 8 ++ 3 files changed, 78 insertions(+), 28 deletions(-) diff --git a/src/client/spark/view/Controls/TickUpDown/TickUpDown.xaml b/src/client/spark/view/Controls/TickUpDown/TickUpDown.xaml index 5b356eccb..dc2a0f70d 100644 --- a/src/client/spark/view/Controls/TickUpDown/TickUpDown.xaml +++ b/src/client/spark/view/Controls/TickUpDown/TickUpDown.xaml @@ -337,10 +337,9 @@ VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" > diff --git a/src/client/spark/view/Controls/TickUpDown/TickUpDownNullableUInt.cs b/src/client/spark/view/Controls/TickUpDown/TickUpDownNullableUInt.cs index 9fb4c85e8..026c20f50 100644 --- a/src/client/spark/view/Controls/TickUpDown/TickUpDownNullableUInt.cs +++ b/src/client/spark/view/Controls/TickUpDown/TickUpDownNullableUInt.cs @@ -1,45 +1,88 @@ -using System.Windows; +using mbts.ui.extensions; +using System; +using System.Windows; using System.Windows.Controls; +using System.Windows.Input; namespace mbts.ui.controls { - public class TickUpDownNullableUInt : Control { - static TickUpDownNullableUInt() => - DefaultStyleKeyProperty.OverrideMetadata(typeof(TickUpDownNullableUInt), new FrameworkPropertyMetadata(typeof(TickUpDownNullableUInt))); + class TickUpDownNullableUInt : Control { + IDisposable _text_hooks = null, _commit_on_enter_hooks; + TextBox _text_box; - public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(uint?), typeof(TickUpDownNullableUInt), - new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)); - public uint? Value { get => GetValue(ValueProperty) as uint?; set => SetValue(ValueProperty, value); } + static TickUpDownNullableUInt() => + DefaultStyleKeyProperty.OverrideMetadata(typeof(TickUpDownNullableUInt), new FrameworkPropertyMetadata(typeof(TickUpDownNullableUInt))); - public static readonly DependencyProperty IncrementProperty = DependencyProperty.Register("Increment", typeof(uint), typeof(TickUpDownNullableUInt), + #region Value + public static readonly DependencyProperty ValueProperty = + WPFHelper.create_dp( + nameof(Value), + FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, + (o, v) => { + if (o._text_box != null) { + if (uint.TryParse(o._text_box.Text, out uint from_text)) { + if (from_text != v) + o._text_box.Text = v.ToString(); + } else if (v.HasValue) { + o._text_box.Text = v.ToString(); + } + } + }); + + public uint? Value { + get => GetValue(ValueProperty) as uint?; + set => SetValue(ValueProperty, value); + } + #endregion + + public static readonly DependencyProperty IncrementProperty = DependencyProperty.Register("Increment", typeof(uint), typeof(TickUpDownNullableUInt), new PropertyMetadata(1u)); - public uint Increment { get { return (uint)GetValue(IncrementProperty); } set { SetValue(IncrementProperty, value); } } + public uint Increment { get { return (uint)GetValue(IncrementProperty); } set { SetValue(IncrementProperty, value); } } public static readonly DependencyProperty TickDefaultValueProperty = DependencyProperty.Register(nameof(TickDefaultValue), typeof(uint), typeof(TickUpDownNullableUInt), new PropertyMetadata(0u)); public uint TickDefaultValue { get => (uint)GetValue(TickDefaultValueProperty); set => SetValue(TickDefaultValueProperty, value); } - public static readonly DependencyProperty CommitOnEnterProperty = DependencyProperty.Register( - nameof(CommitOnEnter), typeof(bool), typeof(TickUpDownNullableUInt), new PropertyMetadata(false)); + public static readonly DependencyProperty CommitOnEnterProperty = + WPFHelper.create_dp( + nameof(CommitOnEnter), + (o, v) => { + Disposable.dispose(ref o._commit_on_enter_hooks); + if (v) { + o._commit_on_enter_hooks = o.SubscribePreviewKeyDown(Key.Enter, on_enter); + void on_enter() => + o.GetBindingExpression(ValueProperty)?.UpdateSource(); + } + }); public bool CommitOnEnter { get => (bool)GetValue(CommitOnEnterProperty); set => SetValue(CommitOnEnterProperty, value); } public override void OnApplyTemplate() { + Disposable.dispose(ref _text_hooks); var tick_up_down = this.assert_template_child("PART_tick_up_down"); - tick_up_down.TickUpCommand = new DelegateCommand(_tick_up); - tick_up_down.TickDownCommand = new DelegateCommand(_tick_down); - } + _text_box = this.assert_template_child("PART_TextBox"); + _text_box.Text = Value.ToString(); + tick_up_down.TickUpCommand = new DelegateCommand(_tick_up); + tick_up_down.TickDownCommand = new DelegateCommand(_tick_down); + push_text_to_value(); + _text_hooks = _text_box.SubscribeTextChanged((s, e) => push_text_to_value()); + void push_text_to_value() { + if (uint.TryParse(_text_box.Text, out uint i)) { + SetCurrentValue(ValueProperty, i); + } else { + SetCurrentValue(ValueProperty, null); + } + } + } - void _tick_up() { - uint? v = Value; - Value = v.HasValue ? v.Value + Increment : TickDefaultValue; - } + void _tick_up() { + uint? v = Value; + SetCurrentValue(ValueProperty, v.HasValue ? v.Value + Increment : TickDefaultValue); + } - void _tick_down() { - uint? v = Value; - if (v.HasValue) { - // do not tick across 0 - Value = v.Value > Increment ? v.Value - Increment : 0u; - } else Value = TickDefaultValue; - } - } + void _tick_down() { + uint? v = Value; + uint inc = Increment; + SetCurrentValue(ValueProperty, v.HasValue && v.Value > inc ? v.Value - inc : TickDefaultValue); + } + } } \ No newline at end of file diff --git a/src/client/spark/view/Extensions/UIElementExtensions.cs b/src/client/spark/view/Extensions/UIElementExtensions.cs index 1f116031b..cb3db578d 100644 --- a/src/client/spark/view/Extensions/UIElementExtensions.cs +++ b/src/client/spark/view/Extensions/UIElementExtensions.cs @@ -143,6 +143,14 @@ namespace mbts.ui.extensions { return Disposable.Create(() => target.PreviewKeyDown -= callback); } + public static IDisposable SubscribePreviewKeyDown(this UIElement target, Key key, Action callback) { + return SubscribePreviewKeyDown(target, on_preview_key_down); + void on_preview_key_down(object sender, KeyEventArgs e) { + if (e.Key == key) + callback(); + } + } + public static IObservable SubscribePreviewKeyDown(this UIElement target) => Observable.Create(o => { target.PreviewKeyDown += on_preview_key_down;