以编程方式选择下一个列表框项目

发布于 2024-09-11 10:08:47 字数 806 浏览 1 评论 0原文

我正在尝试对两个按钮进行编程以模仿向上/向下箭头键的行为,这意味着当我按下向上按钮时,它会在列表框中向上移动一项,依此类推。我编写了以下代码:

private void mainlistup(object sender, System.Windows.RoutedEventArgs e)
{
    if (listBox_Copy.SelectedIndex != -1 &&
        listBox_Copy.SelectedIndex < listBox_Copy.Items.Count &&
        listBox_Copy.SelectedIndex !=1)
    {
        listBox_Copy.SelectedIndex = listBox_Copy.SelectedIndex - 1;
    }
}

private void mainlistdown(object sender, System.Windows.RoutedEventArgs e)
{
    if (listBox_Copy.SelectedIndex < listBox_Copy.Items.Count &&
       listBox_Copy.SelectedIndex != -1)
    {
        listBox_Copy.SelectedIndex = listBox_Copy.SelectedIndex + 1;
    }
}

这是有效的,但是,当按下按钮时,该项目将失去其选择...选择索引设置正确(绑定到所选项目的其他数据绑定项目显示正确的值),但列表框项目不是不再突出显示。如何设置所选项目突出显示?

I'm trying to program two buttons to imitate the up/down arrow key behavior, meaning that when I press the button for up, it moves up one item in my listbox and so on. I wrote the following code:

private void mainlistup(object sender, System.Windows.RoutedEventArgs e)
{
    if (listBox_Copy.SelectedIndex != -1 &&
        listBox_Copy.SelectedIndex < listBox_Copy.Items.Count &&
        listBox_Copy.SelectedIndex !=1)
    {
        listBox_Copy.SelectedIndex = listBox_Copy.SelectedIndex - 1;
    }
}

private void mainlistdown(object sender, System.Windows.RoutedEventArgs e)
{
    if (listBox_Copy.SelectedIndex < listBox_Copy.Items.Count &&
       listBox_Copy.SelectedIndex != -1)
    {
        listBox_Copy.SelectedIndex = listBox_Copy.SelectedIndex + 1;
    }
}

This works, however, when pressing the button the item loses its selection... The selection index is set properly (other databinded items, binded to selected item show the correct values) but the listbox item isn't highlighted anymore. How do I set the selected item to become highlighted?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天涯离梦残月幽梦 2024-09-18 10:08:47

您的列表框可能刚刚失去焦点。设置 SelectedIndex 后只需执行以下操作:

listBox_Copy.Focus();

Your ListBox has probably just lost focus. Just do the following after setting the SelectedIndex:

listBox_Copy.Focus();
昨迟人 2024-09-18 10:08:47

正如 GenericTypeTea 所说,这听起来很可能与失去焦点有关。然而,另一个问题是您的代码过于复杂,不允许您转到顶部的项目。我建议将其更改为:

向上移动

if (listBox_Copy.SelectedIndex > 0)
{ 
     listBox_Copy.SelectedIndex = listBox_Copy.SelectedIndex - 1; 
}

向下移动

if (listBox_Copy.SelectedIndex < listBox_Copy.Items.Count - 1)
{
     listBox_Copy.SelectedIndex = listBox_Copy.SelectedIndex + 1;
}            

As GenericTypeTea says, it sounds likely that it's to do with lost focus. Another issue however is that your code is overcomplicated and won't let you go up to the item at the top. I'd suggest changing it to something like:

Move up

if (listBox_Copy.SelectedIndex > 0)
{ 
     listBox_Copy.SelectedIndex = listBox_Copy.SelectedIndex - 1; 
}

Move down

if (listBox_Copy.SelectedIndex < listBox_Copy.Items.Count - 1)
{
     listBox_Copy.SelectedIndex = listBox_Copy.SelectedIndex + 1;
}            
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文