C# WPF-ScrollViewer在调用ScrollToHorizontalOffset()时不会滚动
我在TabControl中有一个Scrollviewer,由于某种原因,当您更改选项卡时,我的Scrollviewer会跳到12的水平偏移量。我的ScrollViewer还有一个高度不受限制的ListView。这是一个奇怪的布局,但是滚动器中的所有内容都必须是可打印的,所以我不能轻易地允许ListView自己滚动 因此,我在TabControl的SelectionChanged事件中尝试了以下几行:C# WPF-ScrollViewer在调用ScrollToHorizontalOffset()时不会滚动,c#,.net,wpf,xaml,scrollviewer,C#,.net,Wpf,Xaml,Scrollviewer,我在TabControl中有一个Scrollviewer,由于某种原因,当您更改选项卡时,我的Scrollviewer会跳到12的水平偏移量。我的ScrollViewer还有一个高度不受限制的ListView。这是一个奇怪的布局,但是滚动器中的所有内容都必须是可打印的,所以我不能轻易地允许ListView自己滚动 因此,我在TabControl的SelectionChanged事件中尝试了以下几行: Console.WriteLine(Scroller.HorizontalOffset); Sc
Console.WriteLine(Scroller.HorizontalOffset);
Scroller.ScrollToHorizontalOffset(0);
Console.WriteLine(Scroller.HorizontalOffset);
他们为WriteLine()的两个调用打印了12个。。。它也不会移动,但您可以从实际UI滚动
有人知道是什么导致了这种奇怪的行为吗
我将尝试寻找合适的代码发布,但这是一个大型项目,可能很难在示例解决方案中重新创建
其他资料:
- 只有当内容足够大且可以水平滚动时,才会出现问题
- 我的水平和垂直滚动条可见性是自动的,但是如果将其设置为隐藏或可见,问题仍然会发生
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window11" Height="300" Width="300">
<TabControl>
<TabItem Header="Tab One">
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<ListBox Margin="25,0,0,0" Width="2000">
<ListBoxItem>WOOT WOOT WOOT WOOT</ListBoxItem>
<ListBoxItem>WOOT WOOT WOOT WOOT</ListBoxItem>
<ListBoxItem>WOOT WOOT WOOT WOOT</ListBoxItem>
<ListBoxItem>WOOT WOOT WOOT WOOT</ListBoxItem>
<ListBoxItem>WOOT WOOT WOOT WOOT</ListBoxItem>
<ListBoxItem>WOOT WOOT WOOT WOOT</ListBoxItem>
<ListBoxItem>WOOT WOOT WOOT WOOT</ListBoxItem>
<ListBoxItem>WOOT WOOT WOOT WOOT</ListBoxItem>
<ListBoxItem>WOOT WOOT WOOT WOOT</ListBoxItem>
<ListBoxItem>WOOT WOOT WOOT WOOT</ListBoxItem>
<ListBoxItem>WOOT WOOT WOOT WOOT</ListBoxItem>
<ListBoxItem>WOOT WOOT WOOT WOOT</ListBoxItem>
<ListBoxItem>WOOT WOOT WOOT WOOT</ListBoxItem>
<ListBoxItem>WOOT WOOT WOOT WOOT</ListBoxItem>
<ListBoxItem>WOOT WOOT WOOT WOOT</ListBoxItem>
</ListBox>
</ScrollViewer>
</TabItem>
<TabItem Header="Tab Two">
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<ListBox Margin="25,0,0,0" Width="2000">
<ListBoxItem>WOOT WOOT WOOT WOOT</ListBoxItem>
<ListBoxItem>WOOT WOOT WOOT WOOT</ListBoxItem>
<ListBoxItem>WOOT WOOT WOOT WOOT</ListBoxItem>
<ListBoxItem>WOOT WOOT WOOT WOOT</ListBoxItem>
<ListBoxItem>WOOT WOOT WOOT WOOT</ListBoxItem>
<ListBoxItem>WOOT WOOT WOOT WOOT</ListBoxItem>
<ListBoxItem>WOOT WOOT WOOT WOOT</ListBoxItem>
<ListBoxItem>WOOT WOOT WOOT WOOT</ListBoxItem>
<ListBoxItem>WOOT WOOT WOOT WOOT</ListBoxItem>
<ListBoxItem>WOOT WOOT WOOT WOOT</ListBoxItem>
<ListBoxItem>WOOT WOOT WOOT WOOT</ListBoxItem>
<ListBoxItem>WOOT WOOT WOOT WOOT</ListBoxItem>
<ListBoxItem>WOOT WOOT WOOT WOOT</ListBoxItem>
<ListBoxItem>WOOT WOOT WOOT WOOT</ListBoxItem>
<ListBoxItem>WOOT WOOT WOOT WOOT</ListBoxItem>
<ListBoxItem>WOOT WOOT WOOT WOOT</ListBoxItem>
</ListBox>
</ScrollViewer>
</TabItem>
</TabControl>
</Window>
呜呜呜呜呜呜
呜呜呜呜呜呜
呜呜呜呜呜呜
呜呜呜呜呜呜
呜呜呜呜呜呜
呜呜呜呜呜呜
呜呜呜呜呜呜
呜呜呜呜呜呜
呜呜呜呜呜呜
呜呜呜呜呜呜
呜呜呜呜呜呜
呜呜呜呜呜呜
呜呜呜呜呜呜
呜呜呜呜呜呜
呜呜呜呜呜呜
呜呜呜呜呜呜
呜呜呜呜呜呜
呜呜呜呜呜呜
呜呜呜呜呜呜
呜呜呜呜呜呜
呜呜呜呜呜呜
呜呜呜呜呜呜
呜呜呜呜呜呜
呜呜呜呜呜呜
呜呜呜呜呜呜
呜呜呜呜呜呜
呜呜呜呜呜呜
呜呜呜呜呜呜
呜呜呜呜呜呜
呜呜呜呜呜呜
呜呜呜呜呜呜
在XamlPad中打这个,运行它。单击一个选项卡标题,注意ScrollViewer会自动滚动,对其进行播放,并注意列表框上的“我的边距”是如何滚动到屏幕外的。我如何防止或解决这种行为?在我的情况下,它使我的布局看起来非常简陋。看起来您必须连接到RequestBringIntoView:
private void ListView_RequestBringIntoView(object sender, RequestBringIntoViewEventArgs e)
{
e.Handled = true;
}
这似乎是一个非常简单的解决方案