0

我基本上有一个带有 2 个全景项目的全景控件,其中只包含不同项目的列表。我按照 windows phone 上的教程检测用户何时到达列表末尾,然后显示新项目 -触发(调用)方法,当 scrollviewer 滚动到其末尾时

但是我的问题是两个列表框都需要这个,所以我确实注册了 2 个不同的依赖属性,但是如何将依赖属性与特定的列表框“链接”,因为两者混淆了。

谢谢你

列表框 1

  <ListBox Foreground="Black" 
                         x:Name="nuus"
                         Loaded="nuus_Loaded_1"
                         ItemsSource="{Binding Story}"
                         SelectionChanged="nuus_SelectionChanged_1">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal" Margin="12,2,0,4" Height="105" Width="432">
                                <Image Width="115" Height="115" Source="{Binding ImageUrl}"/>
                                <StackPanel Width="311" Margin="8,0,0,0">
                                    <TextBlock Text="{Binding Title}" 
                                           TextWrapping="Wrap" 
                                           FontWeight="Bold"
                                           Margin="10,0" 
                                           Foreground="Black"
                                           FontSize="20"/>

                                    <TextBlock Text="{Binding CreationDate}" 
                                           TextWrapping="Wrap" 
                                           Foreground="Black"
                                           Margin="10,-2,10,0" 
                                           FontSize="20"/>
                                </StackPanel>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
4

1 回答 1

0

这是一个辅助类,它提取了相同的功能,以便能够更轻松地复制它:

public class ScollViewerEndListener :FrameworkElement
{

    public event EventHandler EndOfScrollReached;

    private ScrollViewer scrollViewer;
    public ScollViewerEndListener()
    {

    }

    public void Init(ScrollViewer scrollViewer)
    {
        this.scrollViewer = scrollViewer;
        var binding = new Binding
        {
            Source = scrollViewer,
            Path = new PropertyPath("VerticalOffset"),
            Mode = BindingMode.OneWay
        };
        SetBinding(ListVerticalOffsetProperty, binding);
    }



    private void OnListVerticalOffsetChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        var atBottom = scrollViewer.VerticalOffset >= scrollViewer.ScrollableHeight;
        if (atBottom)
        {
            if (EndOfScrollReached != null)
            {
                EndOfScrollReached(this, null);
            }
        }
    }

    public readonly DependencyProperty ListVerticalOffsetProperty;

    public double ListVerticalOffset
    {
        get { return (double)GetValue(ListVerticalOffsetProperty); }
        set { SetValue(ListVerticalOffsetProperty, value); }
    }
}

你可以像这样在你的文件中使用:

ScollViewerEndListener scrollListenerFirstListBox=new ScollViewerEndListener ();
ScollViewerEndListener scrollListenerSecondListBox=new ScollViewerEndListener ();

在构造函数中:

scrollListenerFirstListBox.EndOfScrollReached += scrollListenerFirstListBox_EndOfScrollReached;
scrollListenerSecondListBox.EndOfScrollReached += scrollListenerSecondListBox_EndOfScrollReached;

加载第一个列表框时:

 scrollListenerFirstListBox.Init(ScrollViewerFirstListBox);

加载第二个列表框时:

scrollListenerSecondListBox.Init(ScrollViewerSecondListBox);

这是一个从 ListBox 获取滚动查看器的函数:

public ScrollViewer GetScrollViewer(DependencyObject parentElement)
    {
        var count = VisualTreeHelper.GetChildrenCount(parentElement);


        for (int i = 0; i < count; i++)
        {
            var child = VisualTreeHelper.GetChild(parentElement, i);
            if (child is ScrollViewer)
            {
                return child as ScrollViewer;
            }
            else
            {
                child = GetScrollViewer(child);
                if (child != null)
                {
                    return child as ScrollViewer;
                }
            }
        }
        return null;
    }

您应该在 ListBox 的 Loaded 中调用此函数,如下所示:

ScrollViewer scrollViewerListA=this.GetScrollViewer(ListA);

还要提到的另一件事是,如果您要尝试创建无限滚动,推荐的方法是使用windows phone 工具包LongListSelector 并按照本文

于 2013-09-20T17:34:03.883 回答