开发者

Change focus list of textboxes WPF

I have a listbox where the Items are Textboxes. I need to set a key to change the focus to the next textbox and begin the editing of its content. I have cheated a solution sending Key strokes to achieve what I want, for example:

        ((TextBox)listBox1.Items[0]).KeyDown += (object x, KeyEventArgs y) => { 
            if (y.Key == Key.Enter) { 
                InputSimulator开发者_C百科.SimulateKeyDown(VirtualKeyCode.TAB); 
                InputSimulator.SimulateKeyPress(VirtualKeyCode.DOWN); 
                InputSimulator.SimulateKeyDown(VirtualKeyCode.TAB); 
            } 
        };

I use the library InputSimulator found here http://inputsimulator.codeplex.com/ for that approach. I know that this is not the correct way to do it so I'm ask how can i achieve the same using the focus methods. I try with the following code but i get "out of range" error that I don't understand:

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

    }


Here is a really short answer for you. In XAML you can use a style to define a common handler for your listboxes...

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

So you can see all of the text boxes in the list. The actual handler code will look like this...

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

So the basic concept is that you will locate the current text box in the list. Then you will find the next one somehow (tags, names, etc.) and focus it explicitly. Of course you will have to tweak depending on your exact need.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜