检查滚动条位置
本文关键字:位置 滚动条 检查 | 更新日期: 2023-09-27 18:08:39
是否可以检查Scrollviewer是否滚动到底部?XML代码:
<ScrollViewer x:Name="scroll" VerticalScrollBarVisibility="Auto" CanContentScroll="True">
<ListBox x:Name="chat" Height="290" VerticalAlignment="Top" Width="440" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.CanContentScroll="True" >
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Width="410">
<TextBlock TextWrapping="WrapWithOverflow" Margin="0,1">
<Run Text="{Binding Name}" Foreground="{Binding Color}" FontWeight="Bold"/>
<Run Text=": "/>
<Run Text="{Binding Message}"/>
</TextBlock>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>
比较ScrollViewer
的ScrollableHeight
和VerticalOffset
。这是一个双精度,这让我很担心,但这里有一个实际比较的演示。
public class HeightConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
double d1 = (double)values[0], d2 = (double)values[1];
return (d1 == d2 ? "equal" : "not equal");
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication4"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Width="300" Height="200">
<Window.Resources>
<local:HeightConverter x:Key="HeightConverter" />
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource HeightConverter}">
<Binding ElementName="Scroll" Path="VerticalOffset" />
<Binding ElementName="Scroll" Path="ScrollableHeight" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
<ScrollViewer x:Name="Scroll" Height="200" VerticalScrollBarVisibility="Auto" CanContentScroll="True" Grid.Row="1">
<ListBox>
<!-- long list of items -->
</ListBox>
</ScrollViewer>
</Grid>
</Window>