由于这个输入的性质,我建议你创建一个小CustomControl,更具体的是一个TextBox,它能够限制Input并将Text转换为相应的值->一个@987654325 @。
GramTextBox 有一个称为Gram 的Gram,它表示输入的Text 的值,并且可以绑定到ViewModel(注意:由于GramTextBox,绑定必须包含Mode=TwoWay尝试更新绑定的Source)。
代码
public sealed class GramTextBox : TextBox
{
//Constructor
public GramTextBox() : base()
{
Text = "0g"; //Initial value
TextChanged += OnTextChanged;
DataObject.AddPastingHandler(this, OnPaste);
}
//Style override (get the Style of a TextBox for the GramTextBox)
static GramTextBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(GramTextBox), new FrameworkPropertyMetadata(typeof(TextBox)));
}
//Define a DependencyProperty to make it bindable (dont forget 'Mode=TwoWay' due the bound value is updated from this GramTextBox)
[Category("Common"), Description("Converted double value from the entered Text in gram")]
[Browsable(true)]
[Bindable(true)]
public double Gram
{
get { return (double)GetValue(PathDataProperty); }
set { SetCurrentValue(PathDataProperty, value); }
}
public static DependencyProperty PathDataProperty = DependencyProperty.Register("Gram", typeof(double), typeof(GramTextBox), new PropertyMetadata(0d));
//Extract the Gram value when Text has changed
private void OnTextChanged(object sender, TextChangedEventArgs e)
{
ExtractGram(Text);
}
//Suppress space input
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
e.Handled = e.Key == Key.Space;
}
//Check text inputs
protected override void OnPreviewTextInput(TextCompositionEventArgs e)
{
e.Handled = !IsValidText(Text.Insert(CaretIndex, e.Text));
}
//check paste inputs
private void OnPaste(object sender, DataObjectPastingEventArgs e)
{
//Check if pasted object is string
if(e.SourceDataObject.GetData(typeof(string)) is string text)
{
//Check if combined string is valid
if(!IsValidText(Text.Insert(CaretIndex, text))) { e.CancelCommand(); }
}
else { e.CancelCommand(); }
}
//Check valid format for extraction (supports incomplete inputs like 0.m -> 0g)
private bool IsValidText(string text)
{
return Regex.IsMatch(text, @"^([0-9]*?\.?[0-9]*?m?g?)$");
}
//Extract value from entered string
private void ExtractGram(string text)
{
//trim all unwanted characters (only allow 0-9 dots and m or g)
text = Regex.Replace(text, @"[^0-9\.mg]", String.Empty);
//Expected Format -> random numbers, dots and couple m/g
//trim all text after the letter g
text = text.Split('g')[0];
//Expected Format -> random numbers, dots and m
//trim double dots (only one dot is allowed)
text = Regex.Replace(text, @"(?<=\..*)(\.)", String.Empty);
//Expected Format -> random numbers with one or more dots and m
//Check if m is at the end of the string to indicate milli (g was trimmed earlier)
bool isMilli = text.EndsWith("m");
//Remove all m, then only a double number should remain
text = text.Replace("m", String.Empty);
//Expected Format -> random numbers with possible dot
//trim all leading zeros
text = text.TrimStart(new char[] { '0' });
//Expected Format -> random numbers with possible dot
//Check if dot is at the beginning
if (text.StartsWith(".")) { text = $"0{text}"; }
//Expected Format -> random numbers with possible dot
//Check if dot is at the end
if (text.EndsWith(".")) { text = $"{text}0"; }
//Expected Format -> random numbers with possible dot
//Try to convert the remaining String to a Number, if it fails -> 0
Double.TryParse(text, out double result);
//Update Gram Property (divide when necessary)
Gram = (isMilli) ? result / 1000d : result;
}
}
用法
将此Class 放入YOURNAMESPACE 并在XAML 添加命名空间别名
xmlns:cc="clr-namespace:YOURNAMESPACE"
现在GramTextBox可以这样使用
<cc:GramTextBox Gram="{Binding VMDoubleProperty, Mode=TwoWay}" ... />
每当GramTextBox 中的Text 发生变化(例如,来自键盘/粘贴等的有效输入)时,它都会更新ViewModel 中的绑定Property。
备注
旨在将.00g、0.0m、.mg 等无意义的输入设置为Gram Property 至0(如备用值)。
个人笔记
感谢@Pavel 提供PasteHandler
编辑
要在DataGrid 中使用此GramTextBox,您可以覆盖Column 的CellTemplate:
<DataGrid AutoGenerateColumns="False" ... >
<DataGrid.Columns>
<!-- Put some other Columns here like DataGridTextColumn -->
<DataGridTemplateColumn Header="Mass">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<cc:GramTextBox Gram="{Binding VMDoubleProperty, Mode=TwoWay}" ... />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<!-- Put some other Columns here -->
</DataGrid.Columns>
</DataGrid>