0

Listview到达列表末尾时,我正在加载新数据。仅使用鼠标滚轮时似乎还可以,但是使用滚动条时一切都出错了。主要思想是提供连续性列表的效果。为此,我使用ScrollInToView鼠标滚轮工作正常的情况。但万一滚动条不起作用。有谁知道如何修理它?

Xaml部分:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" Name="SomeContol" >
<Window.Resources>
    <DataTemplate x:Key="Item">
        <Grid Grid.Column="1" Height="65" HorizontalAlignment="Left" Name="grid1" VerticalAlignment="Top" Width="239">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="64*" />
                <ColumnDefinition Width="175*" />
            </Grid.ColumnDefinitions>
            <TextBlock Height="30" HorizontalAlignment="Center" Margin="6,17,0,18" Name="textBlock1" Text="{Binding Number}" VerticalAlignment="Center" Width="58" TextAlignment="Center" FontSize="20" FontFamily="Shruti" TextTrimming="WordEllipsis" TextWrapping="Wrap" />
            <Grid.Background>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#FF9D76FF" Offset="0.163" />
                    <GradientStop Color="#08389FFF" Offset="0.878" />
                </LinearGradientBrush>
            </Grid.Background>
        </Grid>
    </DataTemplate>
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="65*" />
        <RowDefinition Height="246*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="264*" />
        <ColumnDefinition Width="239*" />
    </Grid.ColumnDefinitions>
    <ListBox 
        SelectedItem="{Binding ElementName=SomeContol, Path=SelectedOne}" 
        ScrollViewer.ScrollChanged="listBox1_ScrollChanged"
        SelectionChanged="listBox1_SelectionChanged"
        ItemTemplate="{StaticResource Item}" 
        ScrollViewer.CanContentScroll="False" 
        ItemsSource="{Binding ElementName=SomeContol, Path=SomeColection}" 
        Height="246" 
        HorizontalAlignment="Left" 
        Name="listBox1" 
        VerticalAlignment="Top" 
        Width="247" 
        Grid.Row="1" />

</Grid>

后面的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Windows.Controls.Primitives;

namespace WpfApplication1
{
/// <summary>
/// Логика взаимодействия для MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    private DateTime DT = DateTime.Now;


    public ObservableCollection<Axle> SomeColection
{
get { return (ObservableCollection<Axle>)GetValue(SomeColectionProperty); }
set { SetValue(SomeColectionProperty, value); }
}

    public Axle SelectedOne
    {
        get { return (Axle)GetValue(SelectedOneProperty); }
        set { SetValue(SelectedOneProperty, value); }
    }

    // Using a DependencyProperty as the backing store for SelectedOne.  This enables        animation, styling, binding, etc...
    public static readonly DependencyProperty SelectedOneProperty =
        DependencyProperty.Register("SelectedOne", typeof(Axle), typeof(MainWindow), new UIPropertyMetadata(null));



// Using a DependencyProperty as the backing store for SomeColection.  This enables      animation, styling, binding, etc...
public static readonly DependencyProperty SomeColectionProperty =
DependencyProperty.Register("SomeColection", typeof(ObservableCollection<Axle>), typeof(MainWindow), new UIPropertyMetadata(null));

    public int count = 0;

    public MainWindow()
    {
        SomeColection = new ObservableCollection<Axle>();

        for(int i = 0; i < 30 ; i++)
        {
            count++;
            SomeColection.Add(new Axle(count, count + i));

        }

        InitializeComponent();

    }

    private void listBox1_ScrollChanged(object sender, ScrollChangedEventArgs e)
    {
        if (((e.ExtentHeight) == (e.VerticalOffset + e.ViewportHeight)))
        {
            //Go Down


            if (DT < DateTime.Now)
            {
                for (int i = 0; i < 20; i++)
                {
                    count++;
                    SomeColection.RemoveAt(0);
                    SomeColection.Add(new Axle(count, count + i));

                }
                SelectedOne = SomeColection[6];
                //SelectedOne = SomeColection[30 -     (int)System.Math.Round(e.ViewportHeight / 65)];
                DT = DateTime.Now.AddSeconds(1);
            }


        }
        if ((e.VerticalOffset == 0))
        {
            //Go Up
            if (count > 31)
            {

                if(DT<DateTime.Now)
                {
                    for (int i = 0; i < 20; i++)
                    {
                        count--;
                        SomeColection.RemoveAt(29);
                        SomeColection.Insert(0, new Axle(count - 29, i));       
                    }
                    SelectedOne = SomeColection[22];

                    DT = DateTime.Now.AddSeconds(1);
                }
            }
        }
    }

    private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        Selector selector = sender as Selector;
        if (selector is ListBox)
        {
            (selector as ListBox).ScrollIntoView(selector.SelectedItem);
        }
    }
}
}
4

0 回答 0