Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Segmented textbox in WPF

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.

like image 781
Jason Avatar asked Oct 14 '22 17:10

Jason


1 Answers

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.

like image 67
Rick Sladkey Avatar answered Oct 24 '22 23:10

Rick Sladkey