From 729cb5cddced11e85ac75ae4f7056f15f02ced36 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Tue, 9 Apr 2019 12:21:43 -0500 Subject: [PATCH] made TickUpDownNullableInt adopt the textbox behavior/implementation logic of TickUpDownDouble --- .../view/Controls/TickUpDown/TickUpDown.xaml | 3 +- .../TickUpDown/TickUpDownNullableInt.cs | 49 ++++++++++++++++--- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/client/spark/view/Controls/TickUpDown/TickUpDown.xaml b/src/client/spark/view/Controls/TickUpDown/TickUpDown.xaml index 7223db41d..cfd9f3b22 100644 --- a/src/client/spark/view/Controls/TickUpDown/TickUpDown.xaml +++ b/src/client/spark/view/Controls/TickUpDown/TickUpDown.xaml @@ -301,9 +301,10 @@ VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" > diff --git a/src/client/spark/view/Controls/TickUpDown/TickUpDownNullableInt.cs b/src/client/spark/view/Controls/TickUpDown/TickUpDownNullableInt.cs index 8b1c9a8c5..e8294ebf4 100644 --- a/src/client/spark/view/Controls/TickUpDown/TickUpDownNullableInt.cs +++ b/src/client/spark/view/Controls/TickUpDown/TickUpDownNullableInt.cs @@ -1,14 +1,37 @@ -using System.Windows; +using mbts.ui.extensions; +using System; +using System.Windows; using System.Windows.Controls; namespace mbts.ui.controls { - public class TickUpDownNullableInt : Control { + class TickUpDownNullableInt : Control { + IDisposable _text_hooks = null; + TextBox _text_box; + static TickUpDownNullableInt() => DefaultStyleKeyProperty.OverrideMetadata(typeof(TickUpDownNullableInt), new FrameworkPropertyMetadata(typeof(TickUpDownNullableInt))); - public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(int?), typeof(TickUpDownNullableInt), - new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)); - public int? Value { get => GetValue(ValueProperty) as int?; set => SetValue(ValueProperty, value); } + #region Value + public static readonly DependencyProperty ValueProperty = + WPFHelper.create_dp( + nameof(Value), + FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, + (o, v) => { + if (o._text_box != null) { + if (int.TryParse(o._text_box.Text, out int from_text)) { + if (from_text != v) + o._text_box.Text = v.ToString(); + } else if (v.HasValue) { + o._text_box.Text = v.ToString(); + } + } + }); + + public int? Value { + get => GetValue(ValueProperty) as int?; + set => SetValue(ValueProperty, value); + } + #endregion public static readonly DependencyProperty IncrementProperty = DependencyProperty.Register("Increment", typeof(int), typeof(TickUpDownNullableInt), new PropertyMetadata(1)); @@ -19,19 +42,31 @@ namespace mbts.ui.controls { public int TickDefaultValue { get => (int)GetValue(TickDefaultValueProperty); set => SetValue(TickDefaultValueProperty, value); } public override void OnApplyTemplate() { + Disposable.dispose(ref _text_hooks); var tick_up_down = this.assert_template_child("PART_tick_up_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 (int.TryParse(_text_box.Text, out int i)) { + SetCurrentValue(ValueProperty, i); + } else { + SetCurrentValue(ValueProperty, null); + } + } } void _tick_up() { int? v = Value; - Value = v.HasValue ? v.Value + Increment : TickDefaultValue; + SetCurrentValue(ValueProperty, v.HasValue ? v.Value + Increment : TickDefaultValue); } void _tick_down() { int? v = Value; - Value = v.HasValue ? v.Value - Increment : TickDefaultValue; + SetCurrentValue(ValueProperty, v.HasValue ? v.Value - Increment : TickDefaultValue); } } } \ No newline at end of file