我目前使用此代碼創建一個數字僅文本框
的XAML
<TextBox Height="22" HorizontalAlignment="Left" Margin="192,118,0,0" Name="Unit_ID" VerticalAlignment="Top" Width="173" PreviewTextInput="UnitID_PreviewTextInput" TextInput="Unit_ID_TextInput" TextChanged="Unit_ID_TextChanged" />
和th ËC#代碼隱藏
private void UnitID_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
foreach (char c in e.Text)
if (!Char.IsDigit(c))
{
e.Handled = true;
break;
}
是可以通過專門使用XAML來做到這一點?我試圖儘量減少我的.cs文件
我認爲你正在尋找驗證。谷歌「驗證在wpf」http://stackoverflow.com/questions/1346707/validation-in-textbox-in-wpf – JSJ 2012-04-05 08:18:36
http://stackoverflow.com/questions/1268552/how-do-i-get-a -textbox-to-only-accept-numeric -input-in-wpf可能有一些有用的提示。 – weeyoung 2012-04-05 08:41:01