代码之家  ›  专栏  ›  技术社区  ›  Aymeric

如何设置列表框的滚动条位置?

  •  2
  • Aymeric  · 技术社区  · 15 年前

    我正在开发一个silverlight4应用程序,我经常使用列表框和数据绑定。我想做的是将滚动条的位置设置到我的列表框的底部。有没有简单的方法?

    COTO_dg.ScrollIntoView(COTO_dg.Items[COTO_dg.Items.Count - 1]);
    

    2 回复  |  直到 15 年前
        1
  •  2
  •   Alex Paven    15 年前

    您发布的代码可以正常工作,但在项插入到ItemsControl之后就不行了。为了确保给控件足够的时间进行自我更新,可以更容易地使用:

    Dispatcher.BeginInvoke(() => lb.ScrollIntoView(lb.Items.Last());
    

    其中lb是ListBox或任何其他ItemsControl。

    注意:引用是Visual Studio插入的默认引用:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    
        2
  •  0
  •   gkrogers    15 年前

    好吧,找到一个不太干净的东西,但它能用。我会贴出来让其他人看到我是怎么做到的:

    下面是我调用方法的方式:

    Dispatcher.BeginInvoke(new lol(my_method));
    

    我创建了一个匿名委托和相应的方法,我将调用它:

        public delegate void lol();
    
        public my_method()
        {
            COTO_dg.ScrollIntoView(COTO_dg.Items[COTO_dg.Items.Count - 1]);
        }
    

    推荐文章