views:

354

answers:

1

I have a silverlight 3 app with a textbox on the main window and a childwindow that has a list of all the potential textbox values. When I open that childwindow I want it to scroll to the correct one in the list.

I'm trying to do this with the code below...using the ScrollIntoView. It was not working at all until I add the UpdateLayerout(). However it does not seem to work all the time. At times it scrolls but not all the way to the item, it is a few items higher than it should be. The listbox is in an Accordion and the list items use a ItemTemplate\DataTemplate, not sure if that effects anything but thought I'd mention it.

Any ideas what I'm missing in the code below?

What I would like is to scroll the item to the top of the list ....any ideas how to that?

(Or any other suggestions on how to code this better)

Thanks!

for (int index = 0; index < myList.Items.Count; index++) {
        object obj = myList.Items[index];
        var listItem= obj as listItemObject;
        if (listItemObj != null)    {
            if (string.Compare(listItemObj.id, _PastedInId, StringComparison.InvariantCultureIgnoreCase) == 0) {
                selectThisIndex = index;
                scrollToThisItem = obj;
            }
        }
    }
    myList.SelectedIndex = selectThisIndex;
    if (scrollToThisItem != null){
        myList.UpdateLayout();
        myList.ScrollIntoView(scrollToThisItem);
    }
A: 

Consider using the ItemsControlExtensions implementation from the Silverlight Toolkit, available at http://silverlight.codeplex.com/sourcecontrol/network/Show?projectName=Silverlight&amp;changeSetId=47051#637494

This has a ScrollIntoView(FrameworkElement element) method that may help.

Jeff Wilcox