以编程方式滚动 Silverlight ListBox

4

我尝试使用以下方法,但它似乎在数据绑定的列表框上无效。

 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

谢谢,Ricky

更新4/1:经过多次尝试,我确认第一种方法可行。但如果能让第二种方法生效会更好,因为通过该方法可以按百分比滚动。所以非常感谢任何帮助。

1个回答

3

对我来说工作得很好:

<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和代码吗?

你说得对,不知为何第一次尝试没有成功。如果我不知道如何使第二种方法起作用,我会接受你的答案。 - Ricky Supit
可能SL版本存在问题。我有SL 4 RC,可以访问滚动界面。然后添加: listBoxScroller.SetScrollPercent(-1,50);滚动到列表的中间。 - Timores

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接