C# 更改文本框WPF的焦点列表

C# 更改文本框WPF的焦点列表,c#,wpf,textbox,focus,C#,Wpf,Textbox,Focus,我有一个列表框,其中的项目是文本框。我需要设置一个键,将焦点切换到下一个文本框,并开始编辑其内容。 我欺骗了一个解决方案,通过发送按键来实现我的目标,例如: ((TextBox)listBox1.Items[0]).KeyDown += (object x, KeyEventArgs y) => { if (y.Key == Key.Enter) { InputSimulator.SimulateKeyDown(V

我有一个列表框,其中的项目是文本框。我需要设置一个键,将焦点切换到下一个文本框,并开始编辑其内容。 我欺骗了一个解决方案,通过发送按键来实现我的目标,例如:

        ((TextBox)listBox1.Items[0]).KeyDown += (object x, KeyEventArgs y) => { 
            if (y.Key == Key.Enter) { 
                InputSimulator.SimulateKeyDown(VirtualKeyCode.TAB); 
                InputSimulator.SimulateKeyPress(VirtualKeyCode.DOWN); 
                InputSimulator.SimulateKeyDown(VirtualKeyCode.TAB); 
            } 
        };
我使用这里找到的库InputSimulator实现这种方法。 我知道这不是正确的方法,所以我想问如何使用聚焦方法实现同样的效果。我尝试使用以下代码,但出现了我不理解的“超出范围”错误:

private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        for (int i = 0; i < 3; i++)
        {
            listBox1.Items.Add(new TextBox() { 
                TabIndex=i
            });

        }
        for (int i = 0; i < listBox1.Items.Count-1; i++)
        {
            ((TextBox)listBox1.Items[i]).KeyDown += (object x, KeyEventArgs y) => { if (y.Key == Key.Tab) { Keyboard.Focus((TextBox)listBox1.Items[i+1]); } };
        }
        ((TextBox)listBox1.Items[listBox1.Items.Count - 1]).KeyDown += (object x, KeyEventArgs y) => { if (y.Key == Key.Tab) { Keyboard.Focus((TextBox)listBox1.Items[0]); }};

    }
private void Window\u已加载(对象发送方,路由目标)
{
对于(int i=0;i<3;i++)
{
listBox1.Items.Add(新文本框(){
TabIndex=i
});
}
对于(int i=0;i{if(y.Key==Key.Tab){Keyboard.Focus((TextBox)listBox1.Items[i+1]);};
}
((TextBox)listBox1.Items[listBox1.Items.Count-1]).KeyDown+=(对象x,KeyEventArgs y)=>{if(y.Key==Key.Tab){Keyboard.Focus((TextBox)listBox1.Items[0]);};
}

以下是一个非常简短的答案。在XAML中,您可以使用样式为列表框定义公共处理程序

<Window x:Class="Interface.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

  <Window.Resources>
    <Style TargetType="{x:Type TextBox}">
      <EventSetter Event="KeyDown" Handler="TextBox_KeyDown"/>
      <Setter Property="Width" Value="100"/>
    </Style>
  </Window.Resources>


  <ListBox Name="lstBoxList">
    <TextBox>ABC</TextBox>
    <TextBox>DEF</TextBox>
    <TextBox>GHI</TextBox>
  </ListBox>

</Window>

因此,基本概念是,您将在列表中找到当前文本框。然后,您将以某种方式找到下一个(标记、名称等),并明确关注它。当然,您必须根据您的具体需要进行调整。

如果您稍微整理一下示例代码,您可能会找到更多关于您的问题的回答。对于初学者,不要太多地内联您的语句。也许我还不明白为什么我的解决方案是错误的,您的解决方案是有效的,所以我将使用它,谢谢。与此相关,我不知道如何在Windows加载时将焦点设置为第一项。(ListBox1.Items[0]作为文本框)。Focus()不会立即开始编辑。我需要再按一下Tab键。你能告诉我怎么做吗?。请注意,每个文本框都是动态创建的。@Voodoomr:我不认为您的示例是错误的,它只是以一种不太稳定的方式创建的。如果要在窗口加载时聚焦文本框,请尝试在window.Loaded事件或类似事件中进行。对不起,我不能在这里提供更多细节。
private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
  if (e.Key == Key.Enter)
  {
    // Locate current item.
    int current = lstBoxList.Items.IndexOf(sender);

    // Find the next item, or give up if we are at the end.
    int next = current + 1;
    if (next > lstBoxList.Items.Count - 1) { return; }

    // Focus the item.
    (lstBoxList.Items[next] as TextBox).Focus();
  }
}