views:

465

answers:

1

I tried using the following method, but it doesn't seem to work on databound listbox.

 mylistbox.ScrollIntoView(mylistbox.Items[mylistbox.Items.Count - 1])

I also tried to grab the IScrollProvider with no success:

var lbItemAutomation = (ListBoxAutomationPeer)ListBoxAutomationPeer.CreatePeerForElement(mylistbox);
var listBoxScroller = (IScrollProvider)lbItemAutomation.GetPattern(PatternInterface.Scroll);  <-- returns null value

Thanks, Ricky

UPDATE 4/1: After retried, I confirm the first method works. However, It will be nice the get the second method works since you can scroll by percentage through this method. So any help will be appreciated.

A: 

Works fine by me:

<StackPanel Orientation="Horizontal">

    <ListBox x:Name="_lbx" ItemsSource="{Binding SimpleItems}" Height="100"/>
    <Button Content="Scroll" Click="DoScroll" />
</StackPanel>

Code-behind:

in constructor:

SimpleItems = new List<string>{ "hello", "world", "the world", "is coming", "to an end", "in 2012", "or maybe", "sometime", "in the future"};

DataContext = this;

Then:

public List<string> SimpleItems { get; set; }


private void DoScroll(object sender, RoutedEventArgs e) {

    _lbx.ScrollIntoView(_lbx.Items[_lbx.Items.Count - 1]);
}

Could you post your related XAML and code-behind ?

Timores
You are right, for some reason it didn't work on my first try. I'll accept your answer if I don't see how to make the second method works.
Ricky Supit
There may be an issue with the SL version. I have SL 4 RC and I can get at the scroll interface. Then adding: listBoxScroller.SetScrollPercent(-1,50);Scrolls to the middle of the list.
Timores