사용자가 TextBox
에 숫자 값만 입력하기를 원합니다. 텍스트 상자에서 WPF로만 숫자를 사용하도록하려면 어떻게해야합니까?
private void txtType1_KeyPress(object sender, KeyPressEventArgs e)
{
int isNumber = 0;
e.Handled = !int.TryParse(e.KeyChar.ToString(), out isNumber);
}
을하지만 WPF를 사용하는 동안 나는 textbox_KeyPress
이벤트 및 e.KeyChar
을 받고 있지 않다.
WPF의 솔루션은 무엇입니까?
Edit:
는 내가 해결했다! 당신은 유효성 검사 규칙을 사용할 수 있습니다
private void txtName_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
CheckIsNumeric(e);
}
private void CheckIsNumeric(TextCompositionEventArgs e)
{
int result;
if(!(int.TryParse(e.Text, out result) || e.Text == "."))
{
e.Handled = true;
}
}
정말 도움이되었습니다. .. – Falcon
[여기에서보기] (http://stackoverflow.com/questions/1268552/how-do-i-get-a-textbox-to-only-accept-numeric-input-in-wpf) 여기에서 찾을 수 있습니다. 그것에 대한 답변의 좋은 개요. – Pinotek