2010-04-01 57 views
4

我嘗試使用以下方法,但似乎無法在數據綁定列表框上工作。以編程方式滾動Silverlight列表框

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: 之後重,我確認了第一種方法作品。但是,獲得第二種方法會很好,因爲您可以通過此方法按百分比滾動。所以任何幫助將不勝感激。

回答

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和代碼隱藏?

+0

你是對的,由於某種原因,它沒有在我的第一次嘗試。如果我不知道如何使第二種方法有效,我會接受你的答案。 – 2010-04-02 13:18:12

+0

SL版本可能存在問題。我有SL 4 RC,我可以進入滾動界面。然後添加: listBoxScroller.SetScrollPercent(-1,50); 滾動到列表中間。 – Timores 2010-04-02 13:56:43