made TickUpDownNullableInt adopt the textbox behavior/implementation logic of TickUpDownDouble

This commit is contained in:
John Colvin committed 2019-04-09 17:16:09 -05:00
1 parent 220a4e6424
commit 729cb5cddc
2 files changed
+44 -8

No files matched your search

@@ -301,9 +301,10 @@
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
>
<TextBox
Name="PART_TextBox"
u:TextBoxBehavior.EnableArrowKeyTraverse="True"
u:TextBoxBehavior.InputFilter="{u:RegexInputFilter Regex={x:Static SignedInteger, MemberType={u:Nested txt:TextUtils.RegexFilters}}}"
BorderThickness="0"
Text="{Binding Value, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay, Converter={StaticResource NullableIntTextConverter}}"
/>
</c:TickUpDown>
</ControlTemplate>
@@ -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<int?, TickUpDownNullableInt>(
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<TickUpDown>("PART_tick_up_down");
_text_box = this.assert_template_child<TextBox>("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);
}
}
}