2011-01-23 55 views
5

Có ai biết về một điều khiển miễn phí hoặc thương mại WPF mà có thể làm một cái gì đó như thế này:textbox phân đoạn trong WPF

alt text

X nhân vật mỗi hộp, và tự động tabbing vào hộp bên cạnh khi bạn hoàn thành mỗi cái hộp? Tương tự như cách các khóa cấp phép được nhập cho các sản phẩm của Microsoft.

Tôi không nghĩ rằng nó sẽ đặc biệt khó làm từ đầu, nhưng tôi muốn tránh phát minh lại bánh xe nếu một ví dụ tốt về điều này đã tồn tại.

Trả lời

4

WPF cung cấp tất cả những gì bạn cần, ngoại trừ tự động chuyển tab sang điều khiển tiếp theo. Một hành vi có thể cung cấp chức năng đó và kết quả sẽ như thế này:

<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> 

Dưới đây là các lớp KeyText và KeyTextCollection (điều chỉnh để nếm):

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

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

Và đây là hành vi mà thực hiện tự động tab và chọn-on-tập trung:

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

Nếu bạn không quen thuộc với các hành vi, Cài đặt Expression Blend 4 SDK và thêm namespace này:

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

và thêm System.Windows.Interactivity vào dự án của bạn.

+0

cảm ơn - Tôi sẽ thử xem thử xem có cần gì không – Jason

Các vấn đề liên quan