WPF 中的分段文本框

发布于 2024-10-13 19:29:39 字数 225 浏览 0 评论 0原文

有谁知道免费或商业 WPF 控件会执行以下操作:

alt text

每个框 X 个字符,并且自动- 完成每个框后按 Tab 键跳到下一个框?类似于为 Microsoft 产品输入许可证密钥的方式。

我认为从头开始做起来并不是特别困难,但如果已经存在一个很好的例子,我想避免重新发明轮子。

Is anyone aware of a free or commercial WPF control that would do something like this:

alt text

X character per box, and auto-tabbing to the next box as you complete each box? Similar to the way that license keys are entered for Microsoft products.

I don't think it would be particularly hard to do from scratch, but I'd like to avoid reinventing the wheel if a good example of this already exists.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

辞旧 2024-10-20 19:29:39

WPF 提供了您所需的一切,除了自动跳到下一个控件之外。行为可以提供该功能,结果如下所示:

<Grid>
    <Grid.Resources>
        <local:KeyTextCollection x:Key="keys"/>
    </Grid.Resources>
    <StackPanel>
        <ItemsControl ItemsSource="{StaticResource keys}" Focusable="False">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" Background="AliceBlue"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding Text}" Margin="5" MaxLength="4" Width="40">
                        <i:Interaction.Behaviors>
                            <local:TextBoxBehavior AutoTab="True" SelectOnFocus="True"/>
                        </i:Interaction.Behaviors>
                    </TextBox>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </StackPanel>
</Grid>

这是 KeyText 和 KeyTextCollection 类(根据口味进行调整):

class KeyText
{
    public string Text { get; set; }
}

class KeyTextCollection : List<KeyText>
{
    public KeyTextCollection()
    {
        for (int i = 0; i < 4; i++) Add(new KeyText { Text = "" });
    }
}

这是实现自动选项卡和焦点选择的行为:

public class TextBoxBehavior : Behavior<TextBox>
{
    public bool SelectOnFocus
    {
        get { return (bool)GetValue(SelectOnFocusProperty); }
        set { SetValue(SelectOnFocusProperty, value); }
    }

    public static readonly DependencyProperty SelectOnFocusProperty = 
        DependencyProperty.Register("SelectOnFocus", typeof(bool), typeof(TextBoxBehavior), new UIPropertyMetadata(false));

    public bool AutoTab
    {
        get { return (bool)GetValue(AutoTabProperty); }
        set { SetValue(AutoTabProperty, value); }
    }

    public static readonly DependencyProperty AutoTabProperty =
        DependencyProperty.Register("AutoTab", typeof(bool), typeof(TextBoxBase), new UIPropertyMetadata(false));

    protected override void OnAttached()
    {
        AssociatedObject.PreviewGotKeyboardFocus += (s, e) =>
        {
            if (SelectOnFocus)
            {
                Action action = () => AssociatedObject.SelectAll();
                AssociatedObject.Dispatcher.BeginInvoke(action, DispatcherPriority.ContextIdle);
            }
        };
        AssociatedObject.TextChanged += (s, e) =>
        {
            if (AutoTab)
            {
                if (AssociatedObject.Text.Length == AssociatedObject.MaxLength &&
                    AssociatedObject.SelectionStart == AssociatedObject.MaxLength)
                {
                    AssociatedObject.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
                }
            }
        };
    }
}

如果您不熟悉行为,安装 Expression Blend 4 SDK 并添加此命名空间:

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

并将 System.Windows.Interactivity 添加到您的项目中。

WPF provides all you need except auto-tabbing to the next control. A behavior can provide that functionality and the result looks like this:

<Grid>
    <Grid.Resources>
        <local:KeyTextCollection x:Key="keys"/>
    </Grid.Resources>
    <StackPanel>
        <ItemsControl ItemsSource="{StaticResource keys}" Focusable="False">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" Background="AliceBlue"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <TextBox Text="{Binding Text}" Margin="5" MaxLength="4" Width="40">
                        <i:Interaction.Behaviors>
                            <local:TextBoxBehavior AutoTab="True" SelectOnFocus="True"/>
                        </i:Interaction.Behaviors>
                    </TextBox>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </StackPanel>
</Grid>

Here are the KeyText and KeyTextCollection classes (adjust to taste):

class KeyText
{
    public string Text { get; set; }
}

class KeyTextCollection : List<KeyText>
{
    public KeyTextCollection()
    {
        for (int i = 0; i < 4; i++) Add(new KeyText { Text = "" });
    }
}

And here is the behavior that implements auto-tab and select-on-focus:

public class TextBoxBehavior : Behavior<TextBox>
{
    public bool SelectOnFocus
    {
        get { return (bool)GetValue(SelectOnFocusProperty); }
        set { SetValue(SelectOnFocusProperty, value); }
    }

    public static readonly DependencyProperty SelectOnFocusProperty = 
        DependencyProperty.Register("SelectOnFocus", typeof(bool), typeof(TextBoxBehavior), new UIPropertyMetadata(false));

    public bool AutoTab
    {
        get { return (bool)GetValue(AutoTabProperty); }
        set { SetValue(AutoTabProperty, value); }
    }

    public static readonly DependencyProperty AutoTabProperty =
        DependencyProperty.Register("AutoTab", typeof(bool), typeof(TextBoxBase), new UIPropertyMetadata(false));

    protected override void OnAttached()
    {
        AssociatedObject.PreviewGotKeyboardFocus += (s, e) =>
        {
            if (SelectOnFocus)
            {
                Action action = () => AssociatedObject.SelectAll();
                AssociatedObject.Dispatcher.BeginInvoke(action, DispatcherPriority.ContextIdle);
            }
        };
        AssociatedObject.TextChanged += (s, e) =>
        {
            if (AutoTab)
            {
                if (AssociatedObject.Text.Length == AssociatedObject.MaxLength &&
                    AssociatedObject.SelectionStart == AssociatedObject.MaxLength)
                {
                    AssociatedObject.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
                }
            }
        };
    }
}

If you are not familiar with behaviors, Install the Expression Blend 4 SDK and add this namespaces:

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

and add System.Windows.Interactivity to your project.

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