达到 MaxLength 时 XAML 触发自动制表符

发布于 2024-10-05 13:05:18 字数 1082 浏览 3 评论 0原文

当 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失退 2024-10-12 13:05:18

只需在 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));
        }
    }
}

simply do this in your Shell.xaml

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

and in your 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));
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文