我有一个scrollviewer想要在一个动作中将其锁定在特定的滚动位置,并在另一个动作中将其释放。
VerticalScrollMode
会改变
滚动位置。MyScrollViewer.VerticalScrollMode = ScrollMode.Disabled;
VerticalScrollBarVisibility
,这会导致verticaloffset达到顶峰MyScrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Disabled;
想知道是否有一种方法可以将scrollviewer锁定在特定的偏移量吗?
答案 0 :(得分:0)
我已经写了一些代码对其进行测试,看来它可以工作。也许它将给您一个解决问题的想法。您可以简单地锁定scrollviewer,然后将其偏移到要显示的项目,或者只是禁用滚动“稍后”,对我而言,它似乎并没有改变视图。如果您动态添加项目,则VisualTreeView可能会进一步帮助您。
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" MinWidth="200" />
</Grid.ColumnDefinitions>
<ScrollViewer x:Name="SomeScrollViewer">
<StackPanel x:Name="LotsOfItems" HorizontalAlignment="Center" />
</ScrollViewer>
<StackPanel Grid.Column="1">
<Button
x:Name="OffsetButton"
Click="OffsetButton_Click"
Content="Offset" />
<Button
x:Name="LockButton"
Click="LockButton_Click"
Content="Lock/Unlock" />
</StackPanel>
</Grid>
后面的代码
public MainPage()
{
this.InitializeComponent();
for (int i = 0; i < 50; i++)
{
LotsOfItems.Children.Add(new TextBlock() { Text = "Some item in a scrollviewer: " + i.ToString() });
}
}
private void OffsetButton_Click(object sender, RoutedEventArgs e)
{
SomeScrollViewer.ChangeView(0, 250, 1f);
}
private void LockButton_Click(object sender, RoutedEventArgs e)
{
if (SomeScrollViewer.VerticalScrollMode == ScrollMode.Disabled)
{
SomeScrollViewer.VerticalScrollMode = ScrollMode.Enabled;
SomeScrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
}
else
{
SomeScrollViewer.VerticalScrollMode = ScrollMode.Disabled;
SomeScrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
}
}
答案 1 :(得分:0)
这是一种非常简单的方法,但是也许您可以在此基础上实现自己想要的。
public double offset = 0.0;
在禁用滚动条之前保存偏移量:
offset = ScrView.HorizontalOffset;
ScrView.HorizontalScrollBarVisibility = ScrollBarVisibility.Disabled;
禁用后,将偏移值添加回滚动条
ScrView.ChangeView(offset, null, null, false);