2009-05-21 4 views

답변

6

확인 텍스트 상자의를 keyDown 이벤트 및 수행이 도움이 될 것입니다

  // If escape is presses, then close the window. 
      if(Key.Escape == e.Key) 
      { 
       this.Close(); 
      } 
      // Allow alphanumeric and space. 
      if(e.Key >= Key.D0 && e.Key <= Key.D9 || 
       e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9 || 
       e.Key >= Key.A && e.Key <= Key.Z || 
       e.Key == Key.Space) 
      { 
       e.Handled = false; 
      } 
      else 
      { 
       e.Handled = true; 
      } 

      // If tab is presses, then the focus must go to the 
      // next control. 
      if(e.Key == Key.Tab) 
      { 
       e.Handled = false; 
      } 

희망 ......

+0

BTW 핸들러에 대한 서명은 : 개인 무효 OnKeyDownHandler (객체 송신자 KeyEventArgs E) – Jeff

0

문자가 이미 TextBox에 추가되었으므로 KeyDown 이벤트 처리기가이 작업을 수행하는 데 가장 적합하지 않을 수 있습니다. PreviewKeyDown 이벤트에 응답하여 이벤트가 계속되는 것을 막을 수는 있지만 알 수없는 결과가 발생할 수 있습니다.

한 가지 방법은 ValidationRule을 TextBox에 연결하는 것입니다. 이것은 사용자가 캐릭터를 입력하는 것을 멈추지는 않지만 그것이 허락되지 않는다는 것을 알려줄 것입니다. 사용자가 잘못된 문자를 입력

<TextBox> 
    <TextBox.Text> 
     <Binding Path="MyString" 
       UpdateSourceTrigger="PropertyChanged"> 
      <Binding.ValidationRules> 
       <myns:MyValidationRule /> 
      </Binding.ValidationRules> 
     </Binding> 
    </TextBox.Text> 
</TextBox> 

때마다 :

public class MyValidationRule : ValidationRule 
{ 
    public override ValidationResult Validate(object value, CultureInfo cultureInfo) 
    { 
     // Logic to determine if the TextBox contains a valid string goes here 
     // Maybe a reg ex that only matches alphanumerics and spaces 
     if(isValidText) 
     { 
      return ValidationResult.ValidResult; 
     } 
     else 
     { 
      return new ValidationResult(false, "You should only enter an alphanumeric character or space"); 
     } 
    } 
} 

그리고 다음과 같이 XAML에서 사용 : 이런 식으로 뭔가를 얻기 위해 당신이 System.Windows.Controls.ValidationRule에서 파생 된이 작업을 수행하려면 그들은 오류 메시지를 받게됩니다. 희망은 그것이 사용하는 것입니다.

0

로 또한 텍스트 상자의 PreviewTextInput 이벤트를 사용할 수 있습니다.

<TextBox Name="txtName" PreviewTextInput="txtName_PreviewTextInput"/> 

그리고 뒤에 코드에 다음을 추가합니다

private void txtName_PreviewTextInput(object sender, TextCompositionEventArgs e) 
    { 
     foreach (char c in e.Text) 
     { 
      if (!char.IsLetterOrDigit(c)) 
      { 
       e.Handled = true; 
       break; 
      } 
     } 
    } 
0

나는 그에게에서 KeyDown을 사용하는 것이 좋습니다 생각이 donot. Backspace, Delete 및 Tab 키도 처리해야합니다.

대신 : 핸들 PreviewTextInput PasteHandler의

private void OnPreviewTextInput(object sender, TextCompositionEventArgs e) 
    { 
     if (!IsMatch(e.Text)) e.Handled = true; 
    } 

    private bool IsMatch(string input) 
    { 
     return Regex.IsMatch(input, MyRegexString); 
    } 

다음 취해야 관리 : 생성자 행 다음에 추가.

DataObject.AddPastingHandler(TargetTextBoxName, PasteHandler); 

    private void PasteHandler(object sender, DataObjectPastingEventArgs e) 
     { 
      if (!e.DataObject.GetDataPresent(typeof(string))) return; 
      // Allow pasting only numeric 
      var pasteText = e.DataObject.GetData(typeof(string)) as string; 
      if (!IsMatch(pasteText)) 
      { 
       e.CancelCommand(); 
      } 
     } 
+0

정규식 될 것 "^ [A-ZA-Z0-9]" – kkk

관련 문제