直接给码:
<UserControl x:Class="SilverlightApplication1.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" Width="640" Height="300"> <Grid x:Name="LayoutRoot" Background="LightBlue"> <Grid.RowDefinitions> <RowDefinition></RowDefinition> <RowDefinition Height="Auto" MinHeight="30"></RowDefinition> </Grid.RowDefinitions> <ScrollViewer Name="sv" VerticalScrollBarVisibility="Visible" LostMouseCapture="sv_LostMouseCapture"> <Border Background="Azure" BorderBrush="Black" Margin="5" BorderThickness="1" Height="400"></Border> </ScrollViewer> <StackPanel Grid.Row="1" VerticalAlignment="Center" Orientation="Horizontal" HorizontalAlignment="Center"> <Button Content="Test" Click="Button_Click" HorizontalAlignment="Center" Padding="10,0"></Button> <TextBlock x:Name="tb1" Margin="10,0,0,0"></TextBlock> </StackPanel> </Grid> </UserControl>
cs部分:
using System.Windows; using System.Windows.Controls; namespace SilverlightApplication1 { public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { ShowHeight(); } private void sv_LostMouseCapture(object sender, System.Windows.Input.MouseEventArgs e) { ShowHeight(); } void ShowHeight() { tb1.Text = string.Format("ActualHeight:{0},VerticalOffset:{1},ViewportHeight:{2},ExtentHeight:{3}", sv.ActualHeight, sv.VerticalOffset, sv.ViewportHeight, sv.ExtentHeight); } } }
运行结果截图:(已经在图上直接加了注释)
注:ExtentHeight只能在LostMouseCapture事件中才能正确获得。