WPF TabControl/ScrollIntoView 问题
我有一个TabControl,其中两个选项卡包含应始终滚动到底部的列表:
<TabControl>
<TabItem Header="Tab1">
<ScrollViewer VerticalScrollBarVisibility="Auto">
<ListBox x:Name="List1">
<ListBox.ItemTemplate>
<DataTemplate DataType="SampleClass">
<TextBlock Text="{Binding SampleProperty}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>
</TabItem>
<TabItem Header="Tab2">
<ScrollViewer VerticalScrollBarVisibility="Auto">
<ListBox x:Name="List2">
<ListBox.ItemTemplate>
<DataTemplate DataType="OtherSampleClass">
<TextBlock Text="{Binding SampleProperty}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>
</TabItem>
</TabControl>
现在,我设置了事件,以便当绑定更改时,执行以下代码以滚动到列表底部(取决于选项卡,这是更改第一个列表的项目时发生的情况的示例):
ListBox1.ScrollIntoView(items.Last<SampleClass>());
这工作正常。当绑定更改时,ListBox 将按预期滚动到底部。
但是,当我设置相同的代码在选项卡更改时执行(在选项卡更改时滚动到列表底部)时,列表不会按预期滚动到底部(并在顶部滚动显示)。
我尝试挂钩 TabControl 的 SelectionChanged 事件。我的猜测是,执行此事件时布局尚未呈现,因此调用 ScrollIntoView() 不会执行任何操作。
有什么办法解决这个问题吗?
谢谢。
I have a TabControl with two tabs containing lists that should always be scrolled to the bottom:
<TabControl>
<TabItem Header="Tab1">
<ScrollViewer VerticalScrollBarVisibility="Auto">
<ListBox x:Name="List1">
<ListBox.ItemTemplate>
<DataTemplate DataType="SampleClass">
<TextBlock Text="{Binding SampleProperty}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>
</TabItem>
<TabItem Header="Tab2">
<ScrollViewer VerticalScrollBarVisibility="Auto">
<ListBox x:Name="List2">
<ListBox.ItemTemplate>
<DataTemplate DataType="OtherSampleClass">
<TextBlock Text="{Binding SampleProperty}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>
</TabItem>
</TabControl>
Now, I have events set up so that when the binding changes, the following code is executed to scroll to the bottom of the list (depends on the tab, this is an example of what happens when the first list's items are changed):
ListBox1.ScrollIntoView(items.Last<SampleClass>());
This works fine. When the binding is changed, the ListBox scrolls to the bottom as expected.
However, when I set the same code up to execute when tabs are changed (to scroll to bottom of a list when the tabs change), the lists do not scroll to the bottom as expected (and show up scrolled at the top).
I tried hooking into SelectionChanged event of the TabControl. My guess is that the layout isn't yet rendered when this event is executed, so calling ScrollIntoView() does nothing.
Is there any way around this?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用低优先级的 Dispatcher.BeginInvoke 来延迟 ScrollIntoView 调用:
现在,直到所有高于输入优先级的处理完成后,ScrollIntoView 才会真正被调用。
You can delay your ScrollIntoView call using a Dispatcher.BeginInvoke with a low priority:
Now ScrollIntoView won't actually be called until all processing above Input priority has completed.