简体   繁体   English

动态滚动到列表框中的焦点项目

[英]scroll to focused item in listbox dynamically

I have two list box in WPF which looks something like this: 我在WPF有两个列表框,看起来像这样:
在此处输入图片说明


Lets say, the left one is lbLeft and right one is lbRight. 可以说,左边的是lbLeft,右边的是lbRight。
The ">" button adds one selected item from lbLeft to lbRight. “>”按钮会将一个选定的项目从lbLeft添加到lbRight。 And, "<" removes the selected item form lbRight from the list. 并且, “ <”从列表中删除所选项目形式lbRight。
The ">>" adds all the item from lbLeft to lbRight and "<<" clears lbRight. “ >>”将所有项目从lbLeft添加到lbRight,而“ <<”清除lbRight。 When I double click an item from lbLeft, it is added to the lbLeft and that newly added item is focused . 当我双击lbLeft中的项目时,该项目将被添加到lbLeft中,并且该新添加的项目将被聚焦 Also, if i try to add an item from lbLeft which already exists in lbRight, it places a focus in that selected item(so that items are not repeated). 另外,如果我尝试从lbRight中已经存在的lbLeft中添加一个项目,则会将焦点放在所选项目中(这样就不会重复该项目)。 But when lots of item are added in lbRight, I have to manually scroll down to the point where focus is placed. 但是,当在lbRight中添加大量项目时,我必须手动向下滚动到焦点所在的位置。 How can I make the scrolling of listbox automatic to the point where focus is placed? 如何使listbox自动滚动到放置焦点的位置?
I have done the following: 我已经完成以下工作:

    private void select_Click(object sender, RoutedEventArgs e) // > button
    {
        addingItemToSelectedList();
    }

    private void remove_Click(object sender, RoutedEventArgs e) // < button
    {
        if (lbRight.SelectedItem != null) {
            lbRight.Items.RemoveAt(lbRight.SelectedIndex);
        }
    }

    private void selectall_Click(object sender, RoutedEventArgs e) // >> button
    {
        lbRight.Items.Clear();
        foreach (string item in column1) {
            lbRight.Items.Add(item);
        }

    }

private void lbLeft_MouseDoubleClick_1(object sender, MouseButtonEventArgs e)
    {
        addingItemToSelectedList();
    }
    private void addingItemToSelectedList() {
        if (lbLeft.SelectedItem != null)
        {
            string item = lbLeft.SelectedItem.ToString();
            addFocus(item);
        }
    }
    private void addFocus(string item) {
        if (!lbRight.Items.Contains(item))
        {
            lbRight.Items.Add(item);
            lbRight.SelectedIndex = lbRight.Items.Count - 1;
            lbRight.Focus();
        }
        else
        {
            int index = lbRight.Items.IndexOf(item);
            lbRight.SelectedIndex = index;
            lbRight.Focus();
        }
    }
    private void removeall_Click_1(object sender, RoutedEventArgs e) //<< button
    {
        lbRight.ItemsSource = null;
        lbRight.Items.Clear();
    }


column1 in the code is a list of items which populate lbLeft. 代码中的column1是填充lbLeft的项目的列表。
UPDATE: 更新:
I tried to use lbRight.ScrollIntoView(lbRight.SelectedIndex); 我尝试使用lbRight.ScrollIntoView(lbRight.SelectedIndex); But it has no effect 但这没有效果

ScrollIntoView() worked now. ScrollIntoView()现在可以工作了。 What i had to do was: 我要做的是:
lbRight.ScrollIntoView(lbRight.Items[lbRight.SelectedIndex]);
It now passes the actual item rather than index of it. 现在,它传递实际项目而不是其索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM