达到 MaxLength 时 XAML 触发自动制表符
当 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));
}
}
}
How can I incorporate an auto-tab when the MaxLength property is reached into a XAML Trigger, DataTrigger, PropertyTrigger, Style.Trigger, etc. Below are two such options for how I have already accomplished this with a TextBox via code-behind. I'm looking to apply it in a XAML style as well. Thanks.
XAML:
<TextBox x:Name="MyTextBox"
Text="{Binding Path=MyProperty}"
Style="{StaticResource TextBoxStyle}"
MaxLength="5"
TextChanged="MyTextBox_TextChanged">
</TextBox>
Code-Behind for 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));
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需在 Shell.xaml
和 shell.xaml.cs 中执行此操作
simply do this in your Shell.xaml
and in your shell.xaml.cs