以编程方式限制要在加载时显示的列表框项目数

时间:2011-07-22 05:24:36

标签: c# xaml sharepoint-2007 silverlight-2.0

我有一个Silverlight 2.0列表框,可以从自定义列表@ SharePoint 2007中读取数据。如何限制在Page.xaml加载时显示的项目数?

我有@ Page.xaml.cs:

private void ProcessResponse()
        {
            XDocument results = XDocument.Parse(_responseString);

            _StaffNews = (from item in results.Descendants(XName.Get("row", "#RowsetSchema"))

                        //where !item.Element("NewsThumbnail").Attribute("src").Value.EndsWith(".gif")
                        select new StaffNews()
                        {                   
                            Title = item.Attribute("ows_Title").Value,
                            NewsBody = item.Attribute("ows_NewsBody").Value,
                            NewsThumbnail = FormatImageUrl(item.Attribute("ows_NewsThumbnail").Value),
                            DatePublished = item.Attribute("ows_Date_Published").Value,
                            PublishedBy = item.Attribute("ows_PublishedBy").Value,
                        }).ToList();
            this.DataContext = _StaffNews;
            //NewsList.SelectedIndex = -1;            
        }

2 个答案:

答案 0 :(得分:3)

您可以将.Take(20)置于ToList()后面,只从列表中删除20个项目。

答案 1 :(得分:1)

Take方法允许您设置项目的限制。它只会迭代集合,直到达到最大计数。您可以使用它代替ToList(),或者如果_StaffNews被定义为List<T>,只需将它们合并.Take(items).ToList();

private void ProcessResponse()
{    
            var items = 10;
            XDocument results = XDocument.Parse(_responseString);

            _StaffNews = (from item in results.Descendants(XName.Get("row", "#RowsetSchema"))

                    //where !item.Element("NewsThumbnail").Attribute("src").Value.EndsWith(".gif")
                    select new StaffNews()
                    {                   
                        Title = item.Attribute("ows_Title").Value,
                        NewsBody = item.Attribute("ows_NewsBody").Value,
                        NewsThumbnail = FormatImageUrl(item.Attribute("ows_NewsThumbnail").Value),
                        DatePublished = item.Attribute("ows_Date_Published").Value,
                        PublishedBy = item.Attribute("ows_PublishedBy").Value,
                    }).Take(items);
            this.DataContext = _StaffNews;
            //NewsList.SelectedIndex = -1;            
}