【问题标题】:Programmatically Scroll Silverlight ListBox以编程方式滚动 Silverlight 列表框
【发布时间】:2010-04-01 20:34:50
【问题描述】:

我尝试使用以下方法,但它似乎不适用于数据绑定列表框。

 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: 重试后,我确认第一种方法有效。但是,获取第二种方法会很好,因为您可以通过此方法按百分比滚动。因此,我们将不胜感激。

【问题讨论】:

    标签: silverlight listbox scroll


    【解决方案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 和代码隐藏?

    【讨论】:

    • 你是对的,出于某种原因,它在我第一次尝试时不起作用。如果我不知道如何使第二种方法起作用,我会接受你的回答。
    • SL 版本可能存在问题。我有 SL 4 RC,我可以进入滚动界面。然后添加:listBoxScroller.SetScrollPercent(-1,50);滚动到列表的中间。
    猜你喜欢
    • 1970-01-01
    • 2012-09-06
    • 1970-01-01
    • 1970-01-01
    • 2020-07-06
    • 1970-01-01
    • 2011-01-16
    • 2011-01-15
    相关资源
    最近更新 更多