怎样可以捕获在一个ListBox中的第一项KeyUp事件? [英] How can I trap the keyup event on the first item in a ListBox?

查看:174
本文介绍了怎样可以捕获在一个ListBox中的第一项KeyUp事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与它上面的文本框列表框。我想用方向键从列表框的文本框进行导航。

的目的是,如果在列表框的第一项被选择,并且用户按键时,文本框将得到焦点。

我几乎有这个工作,但问题是,当用户按键时,则selectedItem改变的的KeyUp事件引发。这意味着,当用户已经选择在列表框的的第二的项目导航到TextBox发生

怎样可以捕获在ListBox中的第一项KeyUp事件?

 < StackPanel的>
 <文本框名称=TextBox1的>< /文本框>
 <列表框名称=ListBox1中的KeyUp =ListBox_KeyUp>
  &其中; ListBoxItem的>一种所述&; / ListBoxItem的>
  < ListBoxItem的> B< / ListBoxItem的>
  < ListBoxItem的> C< / ListBoxItem的>
 < /列表框>
< / StackPanel的>
 


 私人无效ListBox_KeyUp(对象发件人,KeyEventArgs E)
    {
        如果(e.Key == Key.Up)
        {
            如果(this.ListBox1.SelectedIndex == 0)
                this.TextBox1.Focus();
        }
    }
 

解决方案

假设你真的想要这个,你可以使用 previewKeyDown 如下:

 < StackPanel的>
        <文本框名称=textBox1的/>
        <列表框previewKeyDown =ListBox_ previewKeyDown>
            < ListBoxItem的内容=项目1/>
            < ListBoxItem的内容=项目2/>
            < ListBoxItem的内容=项目3/>
        < /列表框>
    < / StackPanel的>
 

本code-背后:

 私人无效ListBox_ previewKeyDown(对象发件人,KeyEventArgs E)
    {
        如果(发送方是列表框)
        {
            VAR列表框=发件人的列表框;
            如果(listBox.Items.Count大于0)
            {
                如果(e.Key == Key.Up&安培;&安培; listBox.Items.Count大于0&安培;&安培; listBox.SelectedIndex == 0)
                {
                    textBox1.Focus();
                    e.Handled =真实;
                }
            }
        }
    }
 

I have a ListBox with a TextBox above it. I would like to use the arrow keys to navigate from the ListBox to the TextBox.

The intention is that if the first item in the ListBox is selected, and the user keys up, the TextBox will get focus.

I nearly have this working, but the problem is that when the user keys up, the SelectedItem is changed before the KeyUp event is raised. This means that the navigation to the TextBox happens when the user has selected the second item in the ListBox.

How can I trap the keyup event on the first item in a ListBox?

<StackPanel>
 <TextBox Name="TextBox1"></TextBox>
 <ListBox Name="ListBox1" KeyUp="ListBox_KeyUp">
  <ListBoxItem>a</ListBoxItem>
  <ListBoxItem>b</ListBoxItem>
  <ListBoxItem>c</ListBoxItem>
 </ListBox>
</StackPanel>


    private void ListBox_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Up)
        {
            if (this.ListBox1.SelectedIndex == 0)
                this.TextBox1.Focus();
        }
    }

解决方案

Assuming you really want to to this, you can use PreviewKeyDown as follows:

    <StackPanel>
        <TextBox Name="textBox1"/>
        <ListBox PreviewKeyDown="ListBox_PreviewKeyDown">
            <ListBoxItem Content="Item1" />
            <ListBoxItem Content="Item2"/>
            <ListBoxItem Content="Item3"/>
        </ListBox>
    </StackPanel>

with this code-behind:

    private void ListBox_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (sender is ListBox)
        {
            var listBox = sender as ListBox;
            if (listBox.Items.Count > 0)
            {
                if (e.Key == Key.Up && listBox.Items.Count > 0 && listBox.SelectedIndex == 0)
                {
                    textBox1.Focus();
                    e.Handled = true;
                }
            }
        }
    }

这篇关于怎样可以捕获在一个ListBox中的第一项KeyUp事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆