1. <small id='M1EgK'></small><noframes id='M1EgK'>

      <i id='M1EgK'><tr id='M1EgK'><dt id='M1EgK'><q id='M1EgK'><span id='M1EgK'><b id='M1EgK'><form id='M1EgK'><ins id='M1EgK'></ins><ul id='M1EgK'></ul><sub id='M1EgK'></sub></form><legend id='M1EgK'></legend><bdo id='M1EgK'><pre id='M1EgK'><center id='M1EgK'></center></pre></bdo></b><th id='M1EgK'></th></span></q></dt></tr></i><div id='M1EgK'><tfoot id='M1EgK'></tfoot><dl id='M1EgK'><fieldset id='M1EgK'></fieldset></dl></div>

    2. <legend id='M1EgK'><style id='M1EgK'><dir id='M1EgK'><q id='M1EgK'></q></dir></style></legend>

      • <bdo id='M1EgK'></bdo><ul id='M1EgK'></ul>
      <tfoot id='M1EgK'></tfoot>

        WPF:在进入时将焦点移动到 ItemsControl 中的下一个项目

        时间:2023-10-07
          <bdo id='YK8kZ'></bdo><ul id='YK8kZ'></ul>

            <i id='YK8kZ'><tr id='YK8kZ'><dt id='YK8kZ'><q id='YK8kZ'><span id='YK8kZ'><b id='YK8kZ'><form id='YK8kZ'><ins id='YK8kZ'></ins><ul id='YK8kZ'></ul><sub id='YK8kZ'></sub></form><legend id='YK8kZ'></legend><bdo id='YK8kZ'><pre id='YK8kZ'><center id='YK8kZ'></center></pre></bdo></b><th id='YK8kZ'></th></span></q></dt></tr></i><div id='YK8kZ'><tfoot id='YK8kZ'></tfoot><dl id='YK8kZ'><fieldset id='YK8kZ'></fieldset></dl></div>
            <tfoot id='YK8kZ'></tfoot>
              <tbody id='YK8kZ'></tbody>

            <small id='YK8kZ'></small><noframes id='YK8kZ'>

            • <legend id='YK8kZ'><style id='YK8kZ'><dir id='YK8kZ'><q id='YK8kZ'></q></dir></style></legend>
                • 本文介绍了WPF:在进入时将焦点移动到 ItemsControl 中的下一个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  WPF:当用户在 ItemsControl 的 texbox 内按下回车键时,我想将焦点移至 ItemsControl 中下一项的文本框,或者如果用户在最后一项中,则创建一个新的文本框.

                  WPF: When user presses the enter key while inside a texbox in a ItemsControl, I want to move focus to a textbox in the next item in ItemsControl, or create a new one if user was in the last item.

                  为了更清楚:

                  场景 1

                  ItemsControl items:
                  [ textbox in item 1 ] <- user is here
                  [ textbox in item 2 ]
                  [ textbox in item 3 ]
                  

                  回车后:

                  [ textbox in item 1 ]
                  [ textbox in item 2 ] <- user is here
                  [ textbox in item 3 ]
                  

                  场景 2

                  ItemsControl 项:

                  ItemsControl items:

                  [ textbox in item 1 ]
                  [ textbox in item 2 ]
                  [ textbox in item 3 ] <- user is here
                  

                  回车后:

                  [ textbox in item 1 ]
                  [ textbox in item 2 ]
                  [ textbox in item 3 ]
                  [ textbox in item 4 ] <- user is here
                  

                  如果有帮助,这里是项目数据模板的代码:

                  If it helps, here is the code for item data template:

                  <ItemsControl.ItemTemplate>
                      <DataTemplate>
                          <Grid Background="White">
                              <Grid.ColumnDefinitions>
                                  <ColumnDefinition Width="*"/>
                                  <ColumnDefinition Width="32"/>
                              </Grid.ColumnDefinitions>
                              <TextBox Text="{Binding Path=PartName, FallbackValue='----',TargetNullValue='----', NotifyOnSourceUpdated=True}" KeyDown="TextBox_KeyDown"/>
                              <Button Grid.Column="1" FontSize="10" x:Name="DeletePartButton" Click="DeletePartButton_Click" Height="22">Usuń</Button>
                          </Grid>
                      </DataTemplate>
                  </ItemsControl.ItemTemplate>
                  

                  编辑 2:我使用 ItemsControl 是因为不需要选择功能.

                  EDIT 2: I use ItemsControl because selecting feature is not wanted.

                  编辑 3:我找到了部分解决方案.它适用于将焦点移动到下一个元素,但不是新元素(这是这里最重要的功能)

                  EDIT 3: I have found a partial solution. It works for moving a focus to a next element, but not a new one (which is the most important functionality here)

                      private void PartNameTextBox_KeyDown(object sender, KeyEventArgs e)
                      {
                          var box = (TextBox)sender;
                  
                          if (e.Key == Key.Enter)
                          {
                              var part = (PiecePart)box.DataContext;
                              int index = part.ParentPiece.Parts.IndexOf(part);
                              if (index == part.ParentPiece.PartCount - 1)
                              {
                                  part.ParentPiece.Parts.Add(new PiecePart(GetNewPartName(part.ParentPiece)));
                                  bool success = PartListBox.ApplyTemplate();
                                  // try to force wpf to build a visual tree for the new item success = false :(
                              }
                  // throws out of bounds exception if a new item was added (and wasn't added to a visual tree)
                              var el = ((UIElement)VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(VisualTreeHelper.GetChild(PartListBox, 0),0),1),0),0),++index),0),0));
                              el.Focus();
                          }
                      }
                  

                  推荐答案

                  关于TextBox的PreviewKeyDown

                  private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
                  {
                   if (e.Key == Key.Enter)
                    {
                     var txt= sender as TextBox;
                     var selecteditem=FindParent<ListBoxItem>(txt);
                     int index = ListBox.ItemContainerGenerator.IndexFromContainer(selecteditem);
                     if(index<ListBox.Items.Count)
                      {
                      var afterItem=(ListBoxItem)ListBox.ItemContainerGenerator.ContainerFromIndex(index+1);
                      TextBox tbFind = GetDescendantByType(afterItem, typeof (TextBox), "TextBox") as TextBox;
                      if (tbFind != null)
                      {
                       FocusHelper.Focus(tbFind);
                      }
                     }
                    }
                  }
                  
                  public static Visual GetDescendantByType(Visual element, Type type, string name)
                  {
                   if (element == null) return null;
                   if (element.GetType() == type)
                   {
                    FrameworkElement fe = element as FrameworkElement;
                    if (fe != null)
                    {
                       if (fe.Name == name)
                       {
                          return fe;
                       }
                    }
                   }
                  Visual foundElement = null;
                  if (element is FrameworkElement)
                    (element as FrameworkElement).ApplyTemplate();
                  for (int i = 0;
                      i < VisualTreeHelper.GetChildrenCount(element);
                      i++)
                  {
                    Visual visual = VisualTreeHelper.GetChild(element, i) as Visual;
                    foundElement = GetDescendantByType(visual, type, name);
                    if (foundElement != null)
                       break;
                  }
                  return foundElement;
                  }
                  
                  public static T FindParent<T>(DependencyObject child) where T : DependencyObject
                  {
                   //get parent item
                  DependencyObject parentObject = VisualTreeHelper.GetParent(child);
                  
                  //we've reached the end of the tree
                  if (parentObject == null) return null;
                  
                  //check if the parent matches the type we're looking for
                  T parent = parentObject as T;
                  if (parent != null)
                      return parent;
                  else
                      return FindParent<T>(parentObject);
                  }
                  

                  在 TextBox 上设置焦点的另一个助手:

                  One more helper to set the Focus on the TextBox:

                  public static class FocusHelper
                  {
                  public static void Focus(UIElement element)
                  {
                   element.Dispatcher.BeginInvoke(DispatcherPriority.Input, new ThreadStart(delegate()
                   {
                      element.Focus();
                   }));
                  }
                  }
                  

                  这篇关于WPF:在进入时将焦点移动到 ItemsControl 中的下一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:WPF列表框通过单击空白区域删除选择 下一篇:在虚拟化列表框中延迟加载图像

                  相关文章

                  <legend id='phXlR'><style id='phXlR'><dir id='phXlR'><q id='phXlR'></q></dir></style></legend>
                    <bdo id='phXlR'></bdo><ul id='phXlR'></ul>
                • <tfoot id='phXlR'></tfoot>
                  <i id='phXlR'><tr id='phXlR'><dt id='phXlR'><q id='phXlR'><span id='phXlR'><b id='phXlR'><form id='phXlR'><ins id='phXlR'></ins><ul id='phXlR'></ul><sub id='phXlR'></sub></form><legend id='phXlR'></legend><bdo id='phXlR'><pre id='phXlR'><center id='phXlR'></center></pre></bdo></b><th id='phXlR'></th></span></q></dt></tr></i><div id='phXlR'><tfoot id='phXlR'></tfoot><dl id='phXlR'><fieldset id='phXlR'></fieldset></dl></div>

                    1. <small id='phXlR'></small><noframes id='phXlR'>