Programmatically Scroll Silverlight ListBox
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. How开发者_开发知识库ever, 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.
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 ?
精彩评论