【问题标题】:Check ScrollBar position [duplicate]检查滚动条位置[重复]
【发布时间】:2017-02-03 02:40:15
【问题描述】:

是否可以检查 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>

【问题讨论】:

    标签: c# wpf listbox scrollviewer


    【解决方案1】:

    ScrollViewerScrollableHeight 与其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>
    

    【讨论】:

    • 我正在移动滚动条,但 ScrollableHeight 和 VerticalOffset 总是返回:0 :(
    • @Nostradamus 你在做代码隐藏的事情吗?
    • 在代码隐藏中,我只是在新线程中将项目添加到 ListBox。
    • @Nostradamus 检查此链接。在尝试获取值之前,您可以尝试scroll.UpdateLayout()。如果没有可用的代码,很难诊断。 link
    猜你喜欢
    • 2011-07-05
    • 1970-01-01
    • 2015-01-26
    • 2011-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多