4
votes

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.

1

1 Answers

3
votes

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 ?