当 MaxLength 属性到达 XAML 触发器、DataTrigger、PropertyTrigger、Style.Trigger 等时,如何合并自动选项卡。下面是两个这样的选项,说明我已经通过代码隐藏使用 TextBox 完成了此操作。我也希望以 XAML 样式应用它。谢谢。
XAML:
<TextBox x:Name="MyTextBox"
Text="{Binding Path=MyProperty}"
Style="{StaticResource TextBoxStyle}"
MaxLength="5"
TextChanged="MyTextBox_TextChanged">
</TextBox>
WPF 的代码隐藏:
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));
}
}
}