滚动到列表框 wp7 的底部

发布于 2024-12-04 13:26:39 字数 1150 浏览 0 评论 0原文

我有很多项目(0-100)最终需要滚动到包含它的列表框的底部。我尝试过:

ScrollViewer.SetVerticalScrollBarVisibility(listmy, ScrollBarVisibility.Auto);
            listmy.SelectedItem =  listmy.Items.Count-1;
            listmy.ScrollIntoView(listmy.SelectedItem);
            ScrollViewer.SetVerticalScrollBarVisibility(listmy, ScrollBarVisibility.Disabled);

但这对我不起作用。滚动查看器包装列表框和文本框。(列表框垂直滚动处于禁用状态) 。 UPD xaml:

<Grid>

    <ScrollViewer Name="_ScrollViewer" VerticalScrollBarVisibility="Auto">
        <StackPanel Name="stackPanel" Height="auto">
          <ListBox ScrollViewer.VerticalScrollBarVisibility="Disabled"  x:Name="listmy">
            <ListBox.ItemTemplate>
              <DataTemplate>...

和 cs:

listmy.ItemsSource = ((App)Application.Current).DIALOG;
        ScrollViewer.SetVerticalScrollBarVisibility(listmy, ScrollBarVisibility.Auto);
        listmy.SelectedIndex =  listmy.Items.Count-1;
        listmy.ScrollIntoView(listmy.SelectedItem);
        ScrollViewer.SetVerticalScrollBarVisibility(listmy, ScrollBarVisibility.Disabled);

I have many items(0-100) end need to scroll to the bottom of Listbox which contains it.I tried:

ScrollViewer.SetVerticalScrollBarVisibility(listmy, ScrollBarVisibility.Auto);
            listmy.SelectedItem =  listmy.Items.Count-1;
            listmy.ScrollIntoView(listmy.SelectedItem);
            ScrollViewer.SetVerticalScrollBarVisibility(listmy, ScrollBarVisibility.Disabled);

but this doesn't workds for me.The scrollviewer wraps the listbox and textbox.(listbox vertical scroll in disabled state).
UPD xaml:

<Grid>

    <ScrollViewer Name="_ScrollViewer" VerticalScrollBarVisibility="Auto">
        <StackPanel Name="stackPanel" Height="auto">
          <ListBox ScrollViewer.VerticalScrollBarVisibility="Disabled"  x:Name="listmy">
            <ListBox.ItemTemplate>
              <DataTemplate>...

and cs:

listmy.ItemsSource = ((App)Application.Current).DIALOG;
        ScrollViewer.SetVerticalScrollBarVisibility(listmy, ScrollBarVisibility.Auto);
        listmy.SelectedIndex =  listmy.Items.Count-1;
        listmy.ScrollIntoView(listmy.SelectedItem);
        ScrollViewer.SetVerticalScrollBarVisibility(listmy, ScrollBarVisibility.Disabled);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南城旧梦 2024-12-11 13:26:39

我猜想您实际上只想确保 ListBox 的 ScrollBar 始终完全滚动到底部。其他解决方案只是确保最后一行可见(不是同一件事)。

要获得您想要的效果,您可以创建一个简单的子类化 ListBox,如下所示:

    using System.Windows.Controls;
    namespace ScrollBarTest
    {
        public class CustomListBox : ListBox
        {
            public void ScrollToBottom()
            {
                var scrollviewer = GetTemplateChild("ScrollViewer") as ScrollViewer;
                scrollviewer.ScrollToVerticalOffset(scrollviewer.ScrollableHeight);
            }
        }
    }

不要像示例中那样使用外部 ScrollViewer,只需使用子类化的 ListBox

只要需要即可调用 ScrollToBottom() 方法它滚动到最后一行。

子类化的原因是 GetTemplateChild 受到保护,因此无法从派生类外部访问。

I gather you actually want to just ensure the ScrollBar of the ListBox is always fully scrolled to the bottom. The other solutions are only about making sure the last line is visible (not the same thing).

To get the effect you want you can create a simple subclassed ListBox like this:

    using System.Windows.Controls;
    namespace ScrollBarTest
    {
        public class CustomListBox : ListBox
        {
            public void ScrollToBottom()
            {
                var scrollviewer = GetTemplateChild("ScrollViewer") as ScrollViewer;
                scrollviewer.ScrollToVerticalOffset(scrollviewer.ScrollableHeight);
            }
        }
    }

Do not use an outer ScrollViewer as you are in the example, just the subclassed ListBox

Just call the ScrollToBottom() method whenever you want it scrolled to the last line.

The reason for the subclassing is that GetTemplateChild is protected so not accessible from outside of a derived class.

遗心遗梦遗幸福 2024-12-11 13:26:39

怎么样:

var lastItem = listmy.Items[listmy.Items.Count - 1];
listmy.ScrollIntoView(lastItem);

我在一个示例项目中尝试过它,效果非常好!

How about this:

var lastItem = listmy.Items[listmy.Items.Count - 1];
listmy.ScrollIntoView(lastItem);

I tried it on a sample project and it worked great!

葬花如无物 2024-12-11 13:26:39

遇到这个,还没有找到“开箱即用,没有代码隐藏”的解决方案,所以我只是想出了这个类:

using System.Windows.Controls;

/// <summary>
/// A list box which automatically scrolls to the last line if new items were added.
/// </summary>
public class AutoscrollListBox : ListBox
{
    /// <summary>
    /// The on items changed.
    /// </summary>
    /// <param name="e">
    /// The e.
    /// </param>
    protected override void OnItemsChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        this.ScrollDown();
        base.OnItemsChanged(e);
    }

    /// <summary>
    /// Scrolls to the last element.
    /// </summary>
    private void ScrollDown()
    {
        if (this.Items.Count > 0)
        {
            var lastItem = this.Items[this.Items.Count - 1];
            this.ScrollIntoView(lastItem);
        }
    }
}

只需使用这个列表框,不需要额外的“魔法”。

Came across this one and haven't found the "works out of the box no code-behind" solution, so I just came up with this class:

using System.Windows.Controls;

/// <summary>
/// A list box which automatically scrolls to the last line if new items were added.
/// </summary>
public class AutoscrollListBox : ListBox
{
    /// <summary>
    /// The on items changed.
    /// </summary>
    /// <param name="e">
    /// The e.
    /// </param>
    protected override void OnItemsChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        this.ScrollDown();
        base.OnItemsChanged(e);
    }

    /// <summary>
    /// Scrolls to the last element.
    /// </summary>
    private void ScrollDown()
    {
        if (this.Items.Count > 0)
        {
            var lastItem = this.Items[this.Items.Count - 1];
            this.ScrollIntoView(lastItem);
        }
    }
}

Just use this listbox and no additional "magic" is required.

﹂绝世的画 2024-12-11 13:26:39

如果您只是设置列表框的选择索引,它应该可以工作。我尝试了一下,似乎效果很好。

listBox1.SelectedIndex = listBox1.Items.Count - 1;

我已经尝试过了,它滚动到列表框的底部,没有任何问题。

If you simply just set the select index of the ListBox, it should work. I tried it, and it seemed to work fine.

listBox1.SelectedIndex = listBox1.Items.Count - 1;

I've tried that, and it scrolled to the bottom of the ListBox, with no problems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文