我在WPF中使用ScrollView,我的应用程序允许用户点击按钮,然后它会自动滚动滚动视图,我在按钮点击事件中使用ScrollToVerticalOffset(),但是滚动视图没有改变任何东西。
我在互联网上搜索过这个问题,但到目前为止我还不能解决它。
还有一个问题:ScrollToVerticalOffset()采用double作为参数,它可能会滚动到特殊像素,有没有办法滚动到n项(不是像素)?
这是我的代码
<ScrollViewer x:Name="scrollViewerChannelBtns" Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Center"
Background="Transparent" VerticalScrollBarVisibility="Hidden" HorizontalScrollBarVisibility="Hidden"
CanContentScroll="True" ScrollChanged="ScrollViewerChannelBtns_ScrollChanged">
<StackPanel x:Name="channelBtns" Orientation="Vertical">
<ItemsControl x:Name="channelBtnItems" ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<ToggleButton x:Name="tgbChannelName" Width="{Binding Path=ChannelNameBtnWidth}" Height="{Binding Path=ChannelNameBtnHeight}" HorizontalAlignment="Left" VerticalAlignment="Center" IsChecked="{Binding Path=IsChecked, Mode=TwoWay}" Content="{Binding Path=ChannelName}" Tag="{Binding Path=Index}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</ScrollViewer>
C#代码
//The button click event handled
private void BtnScrollDownClicked(object sender, RoutedEventArgs e)
{ scrollViewerChannelBtns.ScrollToVerticalOffset(scrollViewerChannelBtns.VerticalOffset + 50);
}
private void BtnScrollUpClicked(object sender, RoutedEventArgs e)
{ scrollViewerChannelBtns.ScrollToVerticalOffset(scrollViewerChannelBtns.VerticalOffset - 50);
}
非常感谢, T&amp; T公司
答案 0 :(得分:8)
我的工作开始了;
ScrollViewer.UpdateLayout();
ScrollViewer.ScrollToVerticalOffset(outPoint.Y);
答案 1 :(得分:2)
对我来说,这个例子有效:
xmlns:sys="clr-namespace:System;assembly=mscorlib"
<Grid>
<ScrollViewer x:Name="scrollViewerChannelBtns" HorizontalAlignment="Center" Height="100" CanContentScroll="False" VerticalAlignment="Center"
Background="Transparent" VerticalScrollBarVisibility="Auto">
<StackPanel x:Name="channelBtns" Orientation="Vertical">
<ItemsControl x:Name="channelBtnItems">
<ItemsControl.ItemTemplate>
<DataTemplate>
<ToggleButton x:Name="tgbChannelName" Width="40" Height="20" HorizontalAlignment="Left" VerticalAlignment="Center" IsChecked="{x:Null}" Content="Test" />
</DataTemplate>
</ItemsControl.ItemTemplate>
<sys:String>Item 1</sys:String>
<sys:String>Item 2</sys:String>
<sys:String>Item 3</sys:String>
<sys:String>Item 4</sys:String>
<sys:String>Item 5</sys:String>
<sys:String>Item 6</sys:String>
<sys:String>Item 7</sys:String>
<sys:String>Item 8</sys:String>
<sys:String>Item 9</sys:String>
<sys:String>Item 10</sys:String>
</ItemsControl>
</StackPanel>
</ScrollViewer>
<Button Name="Up" Width="50" Height="30" VerticalAlignment="Top" Margin="110,0,0,0" Content="Up" Click="Up_Click" />
<Button Name="Down" Width="50" Height="30" VerticalAlignment="Top" Margin="210,0,0,0" Content="Down" Click="Down_Click" />
</Grid>
在示例中,我设置了ScrollViewer
和CanContentScroll
设置false
的高度。引用回答why setting ScrollViewer.CanContentScroll to false disable virtualization:
ScrollViewer目前允许两种滚动模式:平滑的逐像素滚动(CanContentScroll = false)或逐项逐项滚动(CanContentScroll = true)。目前,WPF仅在按项目滚动时支持UI虚拟化。基于像素的滚动也称为“物理滚动”,基于项目的滚动也称为“逻辑滚动”。
虚拟化需要基于项目的滚动,因此它可以跟踪当前视图中的逻辑单元(项目)...将ScrollViewer设置为基于像素的滚动它们不再是逻辑单元的概念,而只是像素!
Code behind
private void Up_Click(object sender, RoutedEventArgs e)
{
scrollViewerChannelBtns.ScrollToVerticalOffset(scrollViewerChannelBtns.VerticalOffset - 50);
}
private void Down_Click(object sender, RoutedEventArgs e)
{
scrollViewerChannelBtns.ScrollToVerticalOffset(scrollViewerChannelBtns.VerticalOffset + 50);
}
默认情况下不支持滚动元素,因此您必须查看以下链接: