首先,你必須記住小數點(無論是.
或,
)已在CultureInfo
來進行設置。如果您打算將應用程序發佈給更多用戶,我會建議記住這一點。
另一件事是,你的情況是沒有意義的:立足CultureInfo
// check for the keys
if(
(// if numeric between 0 and 9
e.Key >= Windows.System.VirtualKey.Number0
&&
e.Key <= Windows.System.VirtualKey.Number9
)
|| // or
(// numeric from numpad between 0 and 9
e.Key >= Windows.System.VirtualKey.NumberPad0
&&
e.Key <= Windows.System.VirtualKey.NumberPad9
)
|| // or decimal mark
e.Key == Windows.System.VirtualKey.Decimal
)
{
// your logic
}
記住Windows.System.VirtualKey.Decimal
將不會返回小數點(分隔符):
// this has to be always true
e.Key >= Windows.System.VirtualKey.Number0
&&
e.Key <= Windows.System.VirtualKey.Number9
|| // above or below has to be true
e.Key >= Windows.System.VirtualKey.NumberPad0
&& // something from above has to be true and below has to be true
e.Key <= Windows.System.VirtualKey.NumberPad9
|| // or just decimal mark .. ?
e.Key == Windows.System.VirtualKey.Decimal
於是用代碼繼續而是來自數字小鍵盤的小數點。
如果你想使用文化信息(國際申請),你可以在CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator
找到小數標記,然後比較文本輸入。
這不回答這個問題。提問者使用KeyRoutedEventArgs而不是KeyPressEventArgs。 –
希望這個[鏈接](http://stackoverflow.com/questions/24612653/windows-phone-8-1-textbox-character-virtualkey-validation)將有助於@OwenPauling –
@ArunD這個答案與無關問題,並且比有幫助更容易混淆。沒有提及改變'事件'類型,因此會引起許多來自無經驗用戶的問題。 –