2010-12-02 11 views
2

XAMLトリガー、DataTrigger、PropertyTrigger、Style.TriggerなどのMaxLengthプロパティに達したときに、どのように自動タブを組み込むことができますか?コードビハインドを介してTextBoxでこれを実現しました。私はそれをXAMLスタイルでも適用しようとしています。ありがとう。XAMLトリガーMaxLengthに達したときの自動タブ

XAML:WPFのコードビハインド

<TextBox x:Name="MyTextBox" 
      Text="{Binding Path=MyProperty}" 
      Style="{StaticResource TextBoxStyle}" 
      MaxLength="5" 
      TextChanged="MyTextBox_TextChanged"> 
</TextBox> 

private void MyTextBox_TextChanged(object sender, TextChangedEventArgs e) 
{ 
    if (MyTextBox.Text.Length == MyTextBox.MaxLength) 
    { 
     Keyboard.Focus(NextTextBox); 
    } 
} 

private void MyTextBox_PreviewKeyDown(object sender, KeyEventArgs e) 
{ 
    // Auto-tab when maxlength is reached 
     if (((TextBox)sender).MaxLength == ((TextBox)sender).Text.Length) 
     { 
      // move focus 
      var ue = e.OriginalSource as FrameworkElement; 
      e.Handled = true; 
      ue.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); 
     } 
    } 
} 
+0

長さ比較のためにトリガーをコントロールテンプレートに追加できますが、xamlにフォーカスを移動することはできません。コードビハインドの使用には何も問題はありません。 – vorrtex

+0

コードビハインドを使っても問題ありませんが、これを複数のビューに適用して、スタイル内にある種のトリガを使用する方が良いと考えました。あなたが使用するコントロールテンプレートの例を私に見せてもらえますか? – woodyiii

答えて

0

は、単にあなたのShell.xaml

<Style TargetType="TextBox"> 
       <EventSetter Event="TextChanged" Handler="MyTextBox_PreviewKeyDown"/> 
      </Style> 

にし、あなたのシェルでこれを行います。 xaml.cs

private void MyTextBox_PreviewKeyDown(object sender, KeyEventArgs e) 
{ 
    // Auto-tab when maxlength is reached 
     if (((TextBox)sender).MaxLength == ((TextBox)sender).Text.Length) 
     { 
      // move focus 
      var ue = e.OriginalSource as FrameworkElement; 
      e.Handled = true; 
      ue.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); 
     } 
    } 
} 
+0

app.xamlとapp.xaml.csがある可能性があります – WPFKK

関連する問題