我尝试使用以下方法,但它似乎不适用于数据绑定(bind)列表框。
mylistbox.ScrollIntoView(mylistbox.Items[mylistbox.Items.Count - 1])
我也试图捕获 IScrollProvider 但没有成功:
var lbItemAutomation = (ListBoxAutomationPeer)ListBoxAutomationPeer.CreatePeerForElement(mylistbox);
var listBoxScroller = (IScrollProvider)lbItemAutomation.GetPattern(PatternInterface.Scroll); <-- returns null value
谢谢,
瑞奇
更新 4/1:
重试后,我确认第一种方法有效。但是,获取第二种方法会很好,因为您可以通过此方法按百分比滚动。因此,任何帮助将不胜感激。
最佳答案
我工作得很好:
<StackPanel Orientation="Horizontal">
<ListBox x:Name="_lbx" ItemsSource="{Binding SimpleItems}" Height="100"/>
<Button Content="Scroll" Click="DoScroll" />
</StackPanel>
代码隐藏:
在构造函数中:
SimpleItems = new List<string>{ "hello", "world", "the world", "is coming", "to an end", "in 2012", "or maybe", "sometime", "in the future"};
DataContext = this;
然后:
public List<string> SimpleItems { get; set; }
private void DoScroll(object sender, RoutedEventArgs e) {
_lbx.ScrollIntoView(_lbx.Items[_lbx.Items.Count - 1]);
}
你能发布你相关的 XAML 和代码隐藏吗?
关于silverlight - 以编程方式滚动 Silverlight 列表框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2563491/