PriceEntryControl filters user input
This commit is contained in:
1 parent
8bea201396
commit
67b124bc96
5 files changed
+57
-8
No files matched your search
@@ -1,5 +1,6 @@
|
||||
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:u="clr-namespace:mbts.ui"
|
||||
xmlns:c="clr-namespace:mbts.ui.controls"
|
||||
xmlns:usp="clr-namespace:mbts.ui.spark"
|
||||
xmlns:uspcv="clr-namespace:mbts.ui.spark.converters">
|
||||
@@ -21,6 +22,7 @@
|
||||
BorderBrush="{TemplateBinding BorderBrush}"
|
||||
BorderThickness="{TemplateBinding BorderThickness}"
|
||||
Text="{Binding Value, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource TickDisplayTwoWayConverter}, Mode=TwoWay}"
|
||||
u:Behavior.DisablePaste="True"
|
||||
/>
|
||||
</c:TickUpDown>
|
||||
</ControlTemplate>
|
||||
|
||||
@@ -1,17 +1,18 @@
|
||||
using bts.api;
|
||||
using mbts.spark;
|
||||
using mbts.ui.extensions;
|
||||
using mbts.ui.spark.converters;
|
||||
using System;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
using static mbts.ui.utility.text.TextUtils;
|
||||
|
||||
namespace mbts.ui.spark
|
||||
{
|
||||
namespace mbts.ui.spark {
|
||||
/// <summary>
|
||||
/// Interaction logic for PriceEntryControl.xaml
|
||||
/// </summary>
|
||||
class PriceEntryControl : Control
|
||||
{
|
||||
class PriceEntryControl : Control {
|
||||
static PriceEntryControl() =>
|
||||
DefaultStyleKeyProperty.OverrideMetadata(typeof(PriceEntryControl), new FrameworkPropertyMetadata(typeof(PriceEntryControl)));
|
||||
|
||||
@@ -46,6 +47,7 @@ namespace mbts.ui.spark
|
||||
#endregion
|
||||
|
||||
TextBox _text;
|
||||
IDisposable _text_input_hook;
|
||||
|
||||
public PriceEntryControl() {
|
||||
TickUpCommand = new DelegateCommand(_tick_up);
|
||||
@@ -53,7 +55,9 @@ namespace mbts.ui.spark
|
||||
}
|
||||
|
||||
public override void OnApplyTemplate() {
|
||||
_text = GetTemplateChild("_text") as TextBox;
|
||||
_text = this.assert_template_child<TextBox>("_text");
|
||||
Disposable.dispose(ref _text_input_hook);
|
||||
_text_input_hook = _text.SubscribePreviewTextInput((s, e) => e.Handled = !RegexFilters.PriceInput.IsMatch(_text.PeekText(e)));
|
||||
_update_text_binding();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using mbts.ui.utility.text;
|
||||
using System;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
@@ -86,12 +87,22 @@ namespace mbts.ui.extensions {
|
||||
target.PreviewDrop += callback;
|
||||
return Disposable.Create(() => target.PreviewDrop -= callback);
|
||||
}
|
||||
}
|
||||
|
||||
public static IDisposable SubscribePreviewTextInput(this UIElement target, TextCompositionEventHandler callback) {
|
||||
target.PreviewTextInput += callback;
|
||||
return Disposable.Create(() => target.PreviewTextInput -= callback);
|
||||
}
|
||||
}
|
||||
|
||||
public static class ScrollViewerExtensions {
|
||||
public static IDisposable SubscribeScrollChanged(this ScrollViewer target, ScrollChangedEventHandler callback) {
|
||||
target.ScrollChanged += callback;
|
||||
return Disposable.Create(() => target.ScrollChanged -= callback);
|
||||
}
|
||||
}
|
||||
|
||||
public static class TextBoxExtensions {
|
||||
public static string PeekText(this TextBox self, TextCompositionEventArgs e) =>
|
||||
TextUtils.InsertText(self.Text, e.Text, self.CaretIndex);
|
||||
}
|
||||
}
|
||||
@@ -124,5 +124,20 @@ namespace mbts.ui {
|
||||
public static bool GetEatMouseDown(DependencyObject d) => (bool)d.GetValue(EatMouseDownProperty);
|
||||
public static void SetEatMouseDown(DependencyObject d, bool value) => d.SetValue(EatMouseDownProperty, value);
|
||||
#endregion
|
||||
|
||||
#region DisablePaste
|
||||
public static readonly DependencyProperty DisablePasteProperty = DependencyProperty.RegisterAttached(
|
||||
"DisablePaste", typeof(bool), typeof(Behavior), new PropertyMetadata(false, OnDisablePasteChanged));
|
||||
|
||||
static void OnDisablePasteChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
|
||||
if ((bool)e.NewValue)
|
||||
DataObject.AddPastingHandler(d, _disable_paste_pasting_handler);
|
||||
else DataObject.RemovePastingHandler(d, _disable_paste_pasting_handler);
|
||||
}
|
||||
static void _disable_paste_pasting_handler(object sender, DataObjectPastingEventArgs e) => e.CancelCommand();
|
||||
|
||||
public static bool GetDisablePaste(DependencyObject d) => (bool)d.GetValue(DisablePasteProperty);
|
||||
public static void SetDisablePaste(DependencyObject d, bool value) => d.SetValue(DisablePasteProperty, value);
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Globalization;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Windows;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Media;
|
||||
@@ -25,6 +26,22 @@ namespace mbts.ui.utility.text {
|
||||
#pragma warning restore CS0618 // Type or member is obsolete
|
||||
ft.SetFontWeight(FontWeights.Bold);
|
||||
return ft;
|
||||
}
|
||||
}
|
||||
|
||||
public static string InsertText(string current_text, string insertion_text, int insertion_index) {
|
||||
if (current_text == null)
|
||||
return insertion_text;
|
||||
else if (insertion_index == current_text.Length)
|
||||
return current_text + insertion_text;
|
||||
else if (insertion_index == 0)
|
||||
return insertion_text + current_text;
|
||||
else if (insertion_index >= 0 && insertion_index < current_text.Length)
|
||||
return current_text.Substring(0, insertion_index) + insertion_text + current_text.Substring(insertion_index, current_text.Length - insertion_index);
|
||||
else return null;
|
||||
}
|
||||
|
||||
public static class RegexFilters {
|
||||
public static readonly Regex PriceInput = new Regex(@"^-?\d*((-|\.)\d*)?$");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user