gpt4 book ai didi

wpf - 达到 MaxLength 时 XAML 触发器自动选项卡

转载 作者:行者123 更新时间:2023-12-04 19:11:51 29 4
gpt4 key购买 nike

当 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));
}
}
}

最佳答案

只需在您的 Shell.xaml 中执行此操作

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

并在您的 shell.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));
}
}
}

关于wpf - 达到 MaxLength 时 XAML 触发器自动选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4340419/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com