滚动到列表框 wp7 的底部
我有很多项目(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我猜想您实际上只想确保 ListBox 的 ScrollBar 始终完全滚动到底部。其他解决方案只是确保最后一行可见(不是同一件事)。
要获得您想要的效果,您可以创建一个简单的子类化 ListBox,如下所示:
不要像示例中那样使用外部 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:
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
isprotected
so not accessible from outside of a derived class.怎么样:
我在一个示例项目中尝试过它,效果非常好!
How about this:
I tried it on a sample project and it worked great!
遇到这个,还没有找到“开箱即用,没有代码隐藏”的解决方案,所以我只是想出了这个类:
只需使用这个列表框,不需要额外的“魔法”。
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:
Just use this listbox and no additional "magic" is required.
如果您只是设置列表框的选择索引,它应该可以工作。我尝试了一下,似乎效果很好。
我已经尝试过了,它滚动到列表框的底部,没有任何问题。
If you simply just set the select index of the ListBox, it should work. I tried it, and it seemed to work fine.
I've tried that, and it scrolled to the bottom of the ListBox, with no problems.