如何支持Enter键以使焦点移动到列表视图的文本列中

时间:2010-09-19 02:01:30

标签: wpf vb.net listview focus

我创建了以下视图

        <ListView.View>
            <GridView>
                <GridViewColumn Header="Tester"
                                DisplayMemberBinding="{Binding User}" />
                <GridViewColumn Header="Executed On" 
                                DisplayMemberBinding="{Binding ExecutionDate}" />
                <GridViewColumn Header="Comment">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <DockPanel>
                                <TextBox Text="{Binding Comment}" Name="TxtComment"
                                         MinWidth="100" VerticalContentAlignment="Top"
                                         BorderThickness="0" PreviewKeyDown="TxtComment_PreviewKeyDown"/>
                            </DockPanel>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>

现在我想的是,在“评论”字段中输入内容并按“Enter”键后,“评论”字段的下一行将获得焦点。

我添加了以下事件代码“PreviewKeyDown”,但似乎下一整行不仅会聚焦“评论”字段......

    Private Sub TxtComment_PreviewKeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.KeyEventArgs)
    Dim focusRequest As TraversalRequest
    Dim focusedElement As Object = sender

    Select Case e.Key
        Case Key.Enter
            focusRequest = New TraversalRequest(FocusNavigationDirection.Down)
        Case Else
            ' Do nothing
            Return
    End Select
    '  Do not further propagate event
    e.Handled = True
    'Move focus
    DirectCast(focusedElement, UIElement).MoveFocus(focusRequest)
End Sub

希望有人能告诉我如何解决这个问题,^ 0 ^

3 个答案:

答案 0 :(得分:1)

将ListChanged事件添加到ListView以与Enter键盘事件链接,并使用VisualTreeHelper查找textBox。

这是ListView XAML;与你的代码相同,除了我使用ObservableCollection在ListView中加载数据(未显示),我添加了SelectionChanged事件:

<ListView x:Name="myView" ItemsSource="{Binding Path=MyData}"  
          SelectionChanged="myView_SelectionChanged">
  <ListView.View>
    <GridView>
      <GridViewColumn Header="Tester" 
                      DisplayMemberBinding="{Binding User}" />
      <GridViewColumn Header="Executed On"  
                      DisplayMemberBinding="{Binding ExecutionDate}" />
      <GridViewColumn Header="Comment">
        <GridViewColumn.CellTemplate>
          <DataTemplate>
            <DockPanel>
              <TextBox Text="{Binding Comment}" Name="TxtComment" 
                        MinWidth="100" VerticalContentAlignment="Top" 
                        BorderThickness="0" 
                        PreviewKeyDown="TxtComment_PreviewKeyDown"/>
             </DockPanel>
          </DataTemplate>
        </GridViewColumn.CellTemplate>
      </GridViewColumn>
    </GridView>
  </ListView.View>
</ListView>

以下是事件处理程序和在代码绑定中使用visualtreeHelper的辅助函数:

private void TxtComment_PreviewKeyDown(object sender, KeyEventArgs e)
{
   TraversalRequest focusRequest = null;
   TextBox focusedElement = sender as TextBox;

   switch (e.Key)
   {
      case Key.Enter:
         focusRequest = new TraversalRequest(FocusNavigationDirection.Down);
         if ( focusedElement != null )
         {
            //Move focus 
            bool moved = focusedElement.MoveFocus(focusRequest);
            if (moved)
            {
               e.Handled = true;
            }
         }
         break;
      default:
         break;
   }
}

// find the TextBox here
private void myView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
   ListView lv = sender as ListView;
   if ( lv != null)
   {
      ItemContainerGenerator generator = lv.ItemContainerGenerator;
      ListViewItem selectedItem = (ListViewItem) generator.ContainerFromIndex(lv.SelectedIndex);
      TextBox tbFind = GetDescendantByType(selectedItem, typeof (TextBox), "TxtComment") 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;
}

在TextBox上设置Focus的另一个帮助:

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

答案 1 :(得分:0)

适用于vb.net。

    Dim focusRequest As TraversalRequest = Nothing
    Dim focusedElement As TextBox = TryCast(sender, TextBox)

答案 2 :(得分:0)

有一个类似的问题(我需要我的GridView更像是一个DataGrid,因为我的用户的理智)。这与此处发布的其他解决方案类似(我重用了其中一些辅助函数)。只需命名输入元素并连接事件处理程序即可。下面的代码适用于Framework 4.5。如果您正在使用Framework 4,只需将“listView.ItemContainerGenerator.Items”引用更改为“listView.Items”。

    private void FrameworkElement_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key != Key.Enter)
            return;

        var element = sender as FrameworkElement;
        if (element == null)
            return;

        var container = (ListViewItem)GetAnscestorByType<ListViewItem>(element);
        if (container == null)
            return;

        var listView = (ListView)GetAnscestorByType<ListView>(container);
        if (listView == null)
            return;

        var index = listView.ItemContainerGenerator.IndexFromContainer(container);
        if (index < 0 || index >= listView.ItemContainerGenerator.Items.Count - 1)
            return;

        var nextItem = listView.ItemContainerGenerator.Items[index + 1];
        if (nextItem == null)
            return;

        var nextContainer = listView.ItemContainerGenerator.ContainerFromItem(nextItem) as Visual;
        if (nextContainer == null)
            return;

        listView.ScrollIntoView(nextItem);

        var hit = GetDescendantByType(nextContainer, element.GetType(), element.Name) as FrameworkElement;
        if (hit == null)
            return;

        Focus(hit);
    }

    public static void Focus(FrameworkElement element)
    {
        element.Dispatcher.BeginInvoke(DispatcherPriority.Input, new ThreadStart(() => element.Focus()));
    }

    public static Visual GetAnscestorByType<T>(Visual element)
        where T : FrameworkElement
    {
        if (element == null)
            return null;

        var parent = VisualTreeHelper.GetParent(element) as Visual;
        while (parent != null)
        {
            if (parent is T)
                return parent;

            parent = VisualTreeHelper.GetParent(parent) as Visual;
        }
        return null;
    }

    public static Visual GetDescendantByType(Visual element, Type type, string name)
    {
        if (element == null) 
            return null;

        if (element.GetType() == type && ((FrameworkElement)element).Name == name)
            return element;

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