/// <summary>
/// 限制文本框只允许输入小数
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void WaterTextBoxControl_PreviewKeyDown(object sender, KeyEventArgs e)
{
var textBox = sender as TextBox;
if ((e.Key == Key.OemPeriod && textBox.Text.Contains(".")) || e.Key == Key.OemPeriod && string.IsNullOrWhiteSpace(textBox.Text))
{
e.Handled = true;
return;
};
if (!((e.Key >= Key.D0 && e.Key <= Key.D9)
|| e.Key == Key.Delete || e.Key == Key.Back || e.Key == Key.OemPeriod
|| e.Key == Key.Tab || e.Key == Key.OemBackTab
|| e.Key == Key.Left || e.Key == Key.Right || e.Key == Key.Up || e.Key == Key.Down)
|| (e.KeyboardDevice.Modifiers == ModifierKeys.Control && e.Key == Key.A))
{
if (e.KeyboardDevice.Modifiers != ModifierKeys.Control)
{
e.Handled = true;
}
}
}
设置文本框的InputMethod.IsInputMethodEnabled="False" 文本框只允许英文模式