[英]How to find the actual scrollbar height inside a scrollviewer control
I'm trying to make smart scrolling for a canvas that expands in size. 我正在尝试对尺寸扩大的画布进行智能滚动。 I want the scrollbars to automatically go to the center of their range.
我希望滚动条自动转到其范围的中心。 So I use:
所以我用:
eagleViewer.ScrollToVerticalOffset(drawingSpace.Height/2);
eagleViewer.ScrollToHorizontalOffset(drawingSpace.Width/2);
Which works but when the scrollbars scroll they go beyond the half point, it seems it's top aligned. 哪个可行,但是当滚动条滚动时,它们超出了半点,似乎是顶部对齐的。 So I wanted to subtract the scrollbar Height or Width so the scrollbars would be perfectly centered at the canvas.
所以我想减去滚动条的高度或宽度,以使滚动条在画布上居中。
I read in other posts that I can do this is at 我在其他文章中读到了我可以做到的
SystemParameters.ScrollHeight
or 要么
SystemParameters.ScrollWidth
But how does that work? 但是,如何运作? I have multiple scrollviewers in the window.
我在窗口中有多个滚动查看器。 I want the height or width of the scrollbars of this specific scrollviewer.
我想要此特定scrollviewer的滚动条的高度或宽度。
Any other way I'm not aware off? 还有其他我不知道的方式吗?
Thanks 谢谢
Edit: 编辑:
Adding XAML section: 添加XAML部分:
<ScrollViewer Name="eagleViewer" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible" Height="Auto" Width="Auto">
<Grid Name="eagleGrid" Background="LightGray">
<Canvas Name="drawingSpace" Background="WhiteSmoke" Width="100" Height="100">
<Canvas.LayoutTransform>
<ScaleTransform CenterX="0" CenterY="0" ScaleX="{Binding ElementName=zoomEagleSlider,Path=Value}" ScaleY="{Binding ElementName=zoomEagleSlider,Path=Value}"/>
</Canvas.LayoutTransform>
</Canvas>
</Grid>
</ScrollViewer>
I add another answer here because I like to post some more code. 我在这里添加了另一个答案,因为我想发布更多代码。 You gave the right keyword: "The canvas height change happens on the line before i run the scrolltoverticaloffset code".
您给了正确的关键字:“在我运行scrolltoverticaloffset代码之前,画布高度发生在行上”。 By doing this I can reproduce the problem.
通过这样做,我可以重现该问题。 The problem is the ScrollViewer obviously evaluates the ScrollableHeight property at a later point.
问题是ScrollViewer显然在以后评估ScrollableHeight属性。 Which means you need to delay using ScrollableHeight.
这意味着您需要延迟使用ScrollableHeight。 It is a bit tricky to find the right point.
找到正确的点有点棘手。 In my following test application I use the first ScrollChanged event after the content's height was changed to center the scrollbar.
在下面的测试应用程序中,在更改内容的高度以使滚动条居中之后,我使用第一个ScrollChanged事件。
<Window x:Class="ScrollViewer.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Width="525"
Height="350">
<Grid>
<ScrollViewer Name="SV"
Margin="71,62,10,10"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto">
<Border x:Name="SVContent" Background="#FFFFA6A6"
BorderBrush="#FF005DFF"
Width="200"
Height="200"
BorderThickness="1" />
</ScrollViewer>
<Button Width="75"
Margin="28,20,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Click="Button_Click"
Content="Button" />
</Grid>
</Window>
cs: CS:
using System;
using System.Windows;
using System.Windows.Controls;
namespace ScrollViewer
{
public partial class MainWindow : Window
{
public MainWindow ()
{
InitializeComponent ();
SV.ScrollChanged += ScrollChangedEventHandler;
}
bool firstScrollAfterContenChanged;
private void Button_Click (object sender, RoutedEventArgs e)
{
firstScrollAfterContenChanged = true;
SVContent.Height = 1000;
}
public void ScrollChangedEventHandler(Object sender, ScrollChangedEventArgs e)
{
if (firstScrollAfterContenChanged)
{
firstScrollAfterContenChanged = false;
SV.ScrollToVerticalOffset (SV.ScrollableHeight / 2);
}
}
}
}
这应该做的工作:
scrollviewer.ScrollToVerticalOffset(scrollviewer.ScrollableHeight/2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.