Programmatically Scroll Silverlight ListBox

I tried using the following method, but it does not work in the data list.

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

I also tried to capture IScrollProvider without success:

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

Thanks Ricky

UPDATE 4/1: After a second review, I confirm that the first method works. However, it will be nice to get the second method, since you can scroll it in percent using this method. Therefore any help would be appreciated.

+4
source share
1 answer

Works well with me:

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

Code for:

in the 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 the associated XAML and code?

+3
source

Source: https://habr.com/ru/post/1305801/


All Articles