mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 06:46:30 +00:00
restrict input for numeric controls
allow input dots
This commit is contained in:
@@ -23,8 +23,10 @@
|
|||||||
TextAlignment="Right"
|
TextAlignment="Right"
|
||||||
VerticalAlignment="Center"
|
VerticalAlignment="Center"
|
||||||
VerticalContentAlignment="Center"
|
VerticalContentAlignment="Center"
|
||||||
Text="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource AncestorType={x:Type local:NumericUpDown}}}"
|
Text="{Binding Value, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type local:NumericUpDown}}}"
|
||||||
LostFocus="NudTextBox_OnLostFocus"
|
LostFocus="NudTextBox_OnLostFocus"
|
||||||
|
PreviewTextInput="NudTextBox_OnPreviewTextInput"
|
||||||
|
DataObject.Pasting="NudTextBox_OnPasting"
|
||||||
/>
|
/>
|
||||||
<RepeatButton
|
<RepeatButton
|
||||||
Name="NudButtonUp"
|
Name="NudButtonUp"
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
using System.Windows.Controls;
|
using System.Windows.Controls;
|
||||||
|
using System.Windows.Input;
|
||||||
|
|
||||||
namespace Azaion.Common.Controls;
|
namespace Azaion.Common.Controls;
|
||||||
|
|
||||||
@@ -98,4 +100,27 @@ public partial class NumericUpDown : UserControl
|
|||||||
|
|
||||||
NudTextBox.SelectionStart = NudTextBox.Text.Length;
|
NudTextBox.SelectionStart = NudTextBox.Text.Length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void NudTextBox_OnPreviewTextInput(object sender, TextCompositionEventArgs e)
|
||||||
|
{
|
||||||
|
var regex = new Regex("[^0-9.]+");
|
||||||
|
e.Handled = regex.IsMatch(e.Text);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void NudTextBox_OnPasting(object sender, DataObjectPastingEventArgs e)
|
||||||
|
{
|
||||||
|
if (e.DataObject.GetDataPresent(typeof(string)))
|
||||||
|
{
|
||||||
|
var text = (string)e.DataObject.GetData(typeof(string));
|
||||||
|
var regex = new Regex("[^0-9.]+");
|
||||||
|
if (regex.IsMatch(text))
|
||||||
|
{
|
||||||
|
e.CancelCommand();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
e.CancelCommand();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user