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

C# 以编程方式限制加载时显示的列表框项目数,c#,xaml,sharepoint-2007,silverlight-2.0,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.De

我有一个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;            
        }
您可以将.Take20放在ToList后面以仅从列表中获取20项。

您可以将.Take20放在ToList后面以仅从列表中获取20项。

方法允许您对项目设置限制。它将只迭代集合,直到达到最大计数。您可以直接使用它而不是ToList,或者如果StaffNews被定义为List,只需将它们组合起来即可

方法允许您设置项目的限制。它将只迭代集合,直到达到最大计数。您可以直接使用它而不是ToList,或者如果StaffNews被定义为List,只需将它们组合起来即可


感谢X的响应:>我收到错误:无法将类型“System.Collections.Generic.Inumerable”隐式转换为System.Collections.Generic.List。存在显式转换。是否缺少强制转换?当我做一个收费表的时候;请把灯给我看看,谢谢。不知何故,我遇到了另一种解决我遇到的错误的方法。简单地说,我做了一个:ToList.GetRange0,3@Mike,不要使用这种方法,它将获取整个数据库,然后只读取前4行。只需使用Take4.来欣赏它,您的方法也会起作用!:>如果可能的话,你介意看看另一个需要你专业知识的小问题吗?谢谢感谢X的响应:>我收到错误:无法将类型“System.Collections.Generic.Inumerable”隐式转换为System.Collections.Generic.List。存在显式转换。是否缺少强制转换?当我做一个收费表的时候;请把灯给我看看,谢谢。不知何故,我遇到了另一种解决我遇到的错误的方法。简单地说,我做了一个:ToList.GetRange0,3@Mike,不要使用这种方法,它将获取整个数据库,然后只读取前4行。只需使用Take4.来欣赏它,您的方法也会起作用!:>如果可能的话,你介意看看另一个需要你专业知识的小问题吗?谢谢嘿,谢谢你的帮助!再一次,我从你们身上学到了很多:嘿,感谢你们的帮助!再一次,我从你们身上学到了很多:
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;            
}