开发者

Silverlight listbox binding question

I have a list which is bound to an observable collection of custom class. Works perfect.

What I want to do us add a row in the last which is not a part of observable collection whenever it comes开发者_如何学运维 into view it should trigger a function to load more items in to listbox.

Thanks Erphan Rajput


The ListBox default template looks something like this:

<Border ...>
  <ScrollViewer x:Name="ScrollViewer" ...>
    <ItemsPresenter />
  </ScrollViewer>
</Border>

The ItemsPresenter is the one that renders the elements from the ItemsSource.

What about you override the default template to something like:

<Border ...>
  <ScrollViewer x:Name="ScrollViewer" ...>
    <StackPanel>
      <ItemsPresenter ... />
      <!-- Your Control Here -->
    </StackPanel>
  </ScrollViewer>
</Border>


I have figured out a way to do this please suggest if this is the right approach considering the current limitations from Silverlight controls.

in XAML:

<ListBox x:Name="MyListBox" ItemsSource="{Binding MyObservableCollection}"
    ItemTemplate="{StaticResource ItemDisplayTemplate}"
    ManipulationCompleted="MyListBox_ManipulationCompleted"/>

in CS:

private void MyListBox_ManipulationCompleted(object sender,
        System.Windows.Input.ManipulationCompletedEventArgs e)
{
    ScrollViewer sv = Utility.FindScrollViewerRecursive((ListBox)sender);
    int a = Int32.Parse(Math.Round(sv.VerticalOffset).ToString()) +
        Int32.Parse(Math.Round(sv.ViewportHeight).ToString());
    if ((a + 1) >= sv.ExtentHeight)
    {
        Debug.WriteLine("Should start loading new items in background");
    }
    Debug.WriteLine(sv.VerticalOffset + " - " + sv.ViewportHeight + " - " + sv.ExtentHeight);
}

I have taken the FindScrollViewerRecursive from here http://blogs.msdn.com/b/rohantha/archive/2010/09/12/silverlight-wp7-list-scroll-with-items-as-image-description-from-web-bing-image-search.aspx

I'll post the full sample source code soon... for now please suggest if this method is OK.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜