made TickUpDownNullableUInt adopt the textbox behavior/implementation logic of TickUpDownDouble
This commit is contained in:
1 parent
7b6bab1767
commit
bde9f6e565
3 files changed
+78
-28
No files matched your search
@@ -337,10 +337,9 @@
|
||||
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
|
||||
>
|
||||
<TextBox
|
||||
u:TextBoxBehavior.CommitOnEnter="{TemplateBinding CommitOnEnter}"
|
||||
Name="PART_TextBox"
|
||||
u:TextBoxBehavior.EnableArrowKeyTraverse="True"
|
||||
BorderThickness="0"
|
||||
Text="{Binding Value, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay, Converter={StaticResource NullableUIntTextConverter}}"
|
||||
/>
|
||||
</c:TickUpDown>
|
||||
</ControlTemplate>
|
||||
|
||||
@@ -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<uint?, TickUpDownNullableUInt>(
|
||||
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<bool, TickUpDownNullableUInt>(
|
||||
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<TickUpDown>("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<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 (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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<KeyEventArgs> SubscribePreviewKeyDown(this UIElement target) =>
|
||||
Observable.Create<KeyEventArgs>(o => {
|
||||
target.PreviewKeyDown += on_preview_key_down;
|
||||
|
||||
Reference in new issue
Block a user