Question
Question

Reputation: 11

scroll down a list box automatically

On a grid, i have a ListBox and a button. If the button is clicked, an item is added to the listbox.

The problem i'm trying to fix is that after the item is added, it is not focused.

I want to automatically scroll down the listbox so that an user can see the item that is lately added. Any thought?

Upvotes: 1

Views: 1685

Answers (2)

Matt Lacey
Matt Lacey

Reputation: 65564

I had to force the call to ScrollIntoView onto the UI thread and this seemed to do the trick.

Here's an example of this working. A dd this as the event handler of an application bar icon button click event in a new DataBound application.

private void ApplicationBarIconButton_Click(object sender, EventArgs e)
{
    App.ViewModel.Items.Add(new ItemViewModel
                                {
                                    LineOne = "new L1",
                                    LineTwo = "new L2",
                                    LineThree = "new L3"
                                });

    Dispatcher.BeginInvoke(() =>
        MainListBox.ScrollIntoView(MainListBox.Items.Last()));
}

Upvotes: 2

Mick N
Mick N

Reputation: 14882

You can set the SelectedIndex property to set the currently selected item.

If it scrolls of the page, you can use ScrollIntoView() to keep the bottom of the list showing.

    listBox1.SelectedIndex = listBox1.Items.Count;
    listBox1.ScrollIntoView(listBox1.SelectedItem);

Upvotes: 2

Related Questions