:
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<TextBox KeyDown="TextBox_KeyDown"/>
</Grid>
그리고에서 KeyDown 이벤트에서
것은 당신이 단지마다 = 진정한 취급 설정하면, 사용자가 입력 할 수있는 일 :
private void TextBox_KeyDown(object sender, KeyRoutedEventArgs e)
{
e.Handled = true;
}
그러나 앞에서 언급 한 것처럼 Back 키를 확인하고 Handled = true로 설정하면 작동하지 않습니다. 사용자는 여전히 백 스페이스를 사용할 수 있습니다. 그래서 이것은 효과가 없습니다.
private void TextBox_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Back)
{
e.Handled = true;
return;
}
}
코드를 디버깅하면 이벤트 핸들러가 실행될 때 문자가 이미 사라짐을 알 수 있습니다. 다른 이벤트를 사용하여이 문제를 해결해야합니다. 여기에 하나 개의 옵션이다 :
XAML :
<TextBox KeyDown="TextBox_KeyDown" KeyUp="TextBox_KeyUp"/>
코드 숨김
private string currentText;
private void TextBox_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Back)
{
if (string.IsNullOrWhiteSpace(currentText))
return;
((TextBox)sender).Text = currentText;
((TextBox)sender).SelectionStart = currentText.Length;
((TextBox)sender).SelectionLength = 0;
}
}
private void TextBox_KeyUp(object sender, KeyRoutedEventArgs e)
{
currentText = ((TextBox)sender).Text;
}
e.Handled = 당신을위한 전체 코드를 게시하시기 바랍니다 수 있습니다, 뭔가 다른 문제를 일으키는 작업을해야 진실이 있어야합니다 추가 도움이 –