-1

Is that possible to check is Scrollviewer scrolled to bottom? My XML code:

<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>
jrbedard
  • 3,662
  • 5
  • 30
  • 34

1 Answers1

0

Compare the ScrollViewer's ScrollableHeight to its VerticalOffset. It's a double, which worried me, but here's a demo of the comparison in action.

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>
Brandon Hood
  • 327
  • 2
  • 12