fix up TickUpDownNullableUShort in the same manner as TickUpDownInt, UInt, etc.
This commit is contained in:
1 parent
c144631759
commit
f1f9503b3a
2 files changed
+98
-23
No files matched your search
@@ -384,6 +384,9 @@
|
||||
</Setter>
|
||||
</Style>
|
||||
<Style TargetType="{x:Type c:TickUpDownNullableUShort}">
|
||||
<Setter Property="Background" Value="{StaticResource TextBox.Static.Background.Brush}"/>
|
||||
<Setter Property="BorderBrush" Value="{StaticResource TextBox.Static.Border.Brush}"/>
|
||||
<Setter Property="BorderThickness" Value="1"/>
|
||||
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
|
||||
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
|
||||
<Setter Property="IsTabStop" Value="False"/>
|
||||
@@ -404,9 +407,10 @@
|
||||
MaxWidth="{TemplateBinding MaxWidth}"
|
||||
>
|
||||
<TextBox
|
||||
x:Name="PART_TextBox"
|
||||
u:TextBoxBehavior.EnableArrowKeyTraverse="True"
|
||||
u:TextBoxBehavior.InputFilter="{u:RegexInputFilter Regex={x:Static UnsignedInteger, MemberType={u:Nested txt:TextUtils.RegexFilters}}}"
|
||||
BorderThickness="0"
|
||||
Text="{Binding Value, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay, Converter={StaticResource NullableUShortTextConverter}}"
|
||||
/>
|
||||
</c:TickUpDown>
|
||||
</ControlTemplate>
|
||||
|
||||
@@ -1,43 +1,114 @@
|
||||
using System.Windows;
|
||||
using mbts.ui.extensions;
|
||||
using mbts.ui.utilities;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace mbts.ui.controls {
|
||||
public class TickUpDownNullableUShort : Control {
|
||||
class TickUpDownNullableUShort : Control {
|
||||
IDisposable _text_hooks = null;
|
||||
TextBox _text_box;
|
||||
// TickUpDown
|
||||
LazyPropagator<TickUpDown, TickUpDownNullableUShort> _explicit_props = new LazyPropagator<TickUpDown, TickUpDownNullableUShort>((t, v) => {
|
||||
t._enable_arrow_key_tick.target = v;
|
||||
t._arrow_key_tick_modifiers.target = v;
|
||||
t._arrow_key_tick_alt_modifiers.target = v;
|
||||
});
|
||||
LazyPropagator<bool, TickUpDown> _enable_arrow_key_tick = new LazyPropagator<bool, TickUpDown>((t, v) => t.EnableArrowKeyTick = v, true);
|
||||
LazyPropagator<ModifierKeys, TickUpDown>
|
||||
_arrow_key_tick_modifiers = new LazyPropagator<ModifierKeys, TickUpDown>((t, v) => t.ArrowKeyTickModifiers = v, ModifierKeys.None),
|
||||
_arrow_key_tick_alt_modifiers = new LazyPropagator<ModifierKeys, TickUpDown>((t, v) => t.ArrowKeyTickAltModifiers = v, ModifierKeys.Alt);
|
||||
|
||||
static TickUpDownNullableUShort() =>
|
||||
DefaultStyleKeyProperty.OverrideMetadata(typeof(TickUpDownNullableUShort), new FrameworkPropertyMetadata(typeof(TickUpDownNullableUShort)));
|
||||
|
||||
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(ushort?), typeof(TickUpDownNullableUShort),
|
||||
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
|
||||
public ushort? Value { get => GetValue(ValueProperty) as ushort?; set => SetValue(ValueProperty, value); }
|
||||
public TickUpDownNullableUShort() =>
|
||||
_explicit_props.target = this;
|
||||
|
||||
public static readonly DependencyProperty IncrementProperty = DependencyProperty.Register("Increment", typeof(ushort), typeof(TickUpDownNullableUShort),
|
||||
new PropertyMetadata((ushort)1));
|
||||
public ushort Increment { get { return (ushort)GetValue(IncrementProperty); } set { SetValue(IncrementProperty, value); } }
|
||||
#region Value
|
||||
public static readonly DependencyProperty ValueProperty =
|
||||
WPFHelper.create_dp<ushort?, TickUpDownNullableUShort>(
|
||||
nameof(Value),
|
||||
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
|
||||
(o, v) => {
|
||||
if (o._text_box != null) {
|
||||
if (ushort.TryParse(o._text_box.Text, out ushort from_text)) {
|
||||
if (from_text != v)
|
||||
o._text_box.Text = v.ToString();
|
||||
} else if (v.HasValue) {
|
||||
o._text_box.Text = v.ToString();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
public static readonly DependencyProperty TickDefaultValueProperty = DependencyProperty.Register(nameof(TickDefaultValue), typeof(ushort), typeof(TickUpDownNullableUShort),
|
||||
new PropertyMetadata((ushort)0));
|
||||
public ushort TickDefaultValue { get => (ushort)GetValue(TickDefaultValueProperty); set => SetValue(TickDefaultValueProperty, value); }
|
||||
public ushort? Value {
|
||||
get => GetValue(ValueProperty) as ushort?;
|
||||
set => SetValue(ValueProperty, value);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Increment
|
||||
public static readonly DependencyProperty IncrementProperty =
|
||||
WPFHelper.create_dp<ushort, TickUpDownNullableUShort>(nameof(Increment), 1);
|
||||
|
||||
public ushort Increment {
|
||||
get => (ushort)GetValue(IncrementProperty);
|
||||
set => SetValue(IncrementProperty, value);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region TickDefaultValue
|
||||
public static readonly DependencyProperty TickDefaultValueProperty =
|
||||
WPFHelper.create_dp<ushort, TickUpDownNullableUShort>(nameof(TickDefaultValue));
|
||||
|
||||
public ushort TickDefaultValue {
|
||||
get => (ushort)GetValue(TickDefaultValueProperty);
|
||||
set => SetValue(TickDefaultValueProperty, value);
|
||||
}
|
||||
#endregion
|
||||
|
||||
// explicit props
|
||||
public bool EnableArrowKeyTick { get => _enable_arrow_key_tick.value; set => _enable_arrow_key_tick.value = value; }
|
||||
public ModifierKeys ArrowKeyTickModifiers { get => _arrow_key_tick_modifiers.value; set => _arrow_key_tick_modifiers.value = value; }
|
||||
public ModifierKeys ArrowKeyTickAltModifiers { get => _arrow_key_tick_alt_modifiers.value; set => _arrow_key_tick_alt_modifiers.value = value; }
|
||||
TickUpDown _tick_up_down { get => _explicit_props.value; set => _explicit_props.value = value; }
|
||||
|
||||
public override void OnApplyTemplate() {
|
||||
var tick_up_down = this.assert_template_child<TickUpDown>("PART_tick_up_down");
|
||||
tick_up_down.TickUpCommand = new DelegateCommand(_tick_up);
|
||||
tick_up_down.TickDownCommand = new DelegateCommand(_tick_down);
|
||||
Disposable.dispose(ref _text_hooks);
|
||||
// TickUpDown
|
||||
_tick_up_down = this.assert_template_child<TickUpDown>("PART_tick_up_down");
|
||||
_tick_up_down.TickUpCommand = new DelegateCommand(_tick_up);
|
||||
_tick_up_down.TickDownCommand = new DelegateCommand(_tick_down);
|
||||
// TextBox
|
||||
_text_box = this.assert_template_child<TextBox>("PART_TextBox");
|
||||
_text_box.Text = Value.ToString();
|
||||
if (_text_box.InputBindings.Count == 0) {
|
||||
// this is a workaround to get KeyBindings to work since the TextBox blocks them while it has keyboard focus.
|
||||
// only do this if count == 0, to avoid interfering with keybindings explicitly set in a custom ControlTemplate.
|
||||
_text_box.InputBindings.AddRange(InputBindings.OfType<KeyBinding>());
|
||||
}
|
||||
push_text_to_value();
|
||||
_text_hooks = _text_box.SubscribeTextChanged((s, e) => push_text_to_value());
|
||||
void push_text_to_value() {
|
||||
if (ushort.TryParse(_text_box.Text, out ushort i)) {
|
||||
SetCurrentValue(ValueProperty, i);
|
||||
} else {
|
||||
SetCurrentValue(ValueProperty, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _tick_up() {
|
||||
ushort? v = Value;
|
||||
if (v.HasValue) {
|
||||
int result = v.Value + Increment;
|
||||
Value = result < ushort.MaxValue ? (ushort)result : ushort.MaxValue;
|
||||
} else Value = TickDefaultValue;
|
||||
SetCurrentValue(ValueProperty, v.HasValue ? v.Value + Increment : TickDefaultValue);
|
||||
}
|
||||
|
||||
void _tick_down() {
|
||||
ushort? v = Value;
|
||||
if (v.HasValue) {
|
||||
// do not tick across 0
|
||||
Value = v.Value > Increment ? (ushort)(v.Value - Increment) : (ushort)0;
|
||||
} else Value = TickDefaultValue;
|
||||
ushort inc = Increment;
|
||||
SetCurrentValue(ValueProperty, v.HasValue && v.Value > inc ? v.Value - inc : TickDefaultValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user