<Grid Background="Blue">
<TextBlock Name="tbl" FontSize="30"></TextBlock>
<StackPanel Margin="600 0 0 0">
<ScrollViewer Name="scrollViewer" Width="200" Height="200" Margin="0 10 0 0" HorizontalAlignment="Left"
IsDeferredScrollingEnabled="False" ViewChanged="scrollViewer_ViewChanged_1" HorizontalScrollMode="Enabled" VerticalScrollMode="Enabled"
HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Visible"
>
<ScrollViewer.Content>
<Image Source="Assets/Logo.png" Width="800"></Image>
</ScrollViewer.Content>
</ScrollViewer>
<StackPanel Orientation="Horizontal">
<Button Content="居中" Click="Button_Click_1"></Button>
</StackPanel>
</StackPanel>
</Grid>
private void scrollViewer_ViewChanged_1(object sender, ScrollViewerViewChangedEventArgs e)
{
this.tbl.Text = "";
this.tbl.Text += "ComputedHorizontalScrollBarVisibility :" + scrollViewer.ComputedHorizontalScrollBarVisibility;
this.tbl.Text += "\r\n";
this.tbl.Text += "ComputedVerticalScrollBarVisibility:" +
scrollViewer.ComputedVerticalScrollBarVisibility;
this.tbl.Text += "\r\n";
this.tbl.Text += "ExtentWidth:" + scrollViewer.ExtentWidth;
this.tbl.Text += "\r\n";
this.tbl.Text += "ExtentHeight:" + scrollViewer.ExtentHeight;
this.tbl.Text += "\r\n";
this.tbl.Text += "ViewportWidth:" + scrollViewer.ViewportWidth;
this.tbl.Text += "\r\n";
this.tbl.Text += "ViewportHeight:" + scrollViewer.ViewportHeight;
this.tbl.Text += "\r\n";
this.tbl.Text += "HorizontalOffset:" + scrollViewer.HorizontalOffset;
this.tbl.Text += "\r\n";
this.tbl.Text += "VerticalOffset:" + scrollViewer.VerticalOffset;
this.tbl.Text += "\r\n";
this.tbl.Text += "ScrollalbeWidth:" + scrollViewer.ScrollableWidth;
this.tbl.Text += "\r\n";
this.tbl.Text += "ScrollableHeight:" + scrollViewer.ScrollableHeight;
this.tbl.Text += "\r\n";
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
scrollViewer.ScrollToHorizontalOffset(scrollViewer.ScrollableWidth / 2);
scrollViewer.ScrollToVerticalOffset(scrollViewer.ScrollableHeight / 2);
}
ComputedHorizontalScrollBarVisibility:是获得水平方向滚动条的状态,Visibility和Collapsed两种情况,当我们设置水平方向的滚动条为
Visibility时,这个值是Visibility,当我们设置Hidden或者Disable,这个值是Collapsed;垂直方向相似。ExtentWidth和ExtentHeight是滚动区域的宽和高,这个宽和高是我们包含内容的宽和高。
ViewportWidth和ViewportHeight可视区域的宽和高,这个一般和scrollViewer的宽和高一致。
HorizontalOffset和VerticalOffset,表示滚动区域水平和竖直方向的便宜量,这两个的最大值和下面两个一样。
ScrollalbeWidth和ScrollableHeight,表示可滚动区域的宽和高。
ScrollToHorizontalOffset()设置水平滚动区域的位置