limit number of listbox items to be displayed on load, programmatically
I have a Silverlight 2.0 listbox which reads in data from a custom list @ SharePoint 2007. How may i limit the number of items to be displayed on load of Page.xaml?
Here i have @ 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()
{ 开发者_JAVA技巧
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;
}
You can put .Take(20)
behind ToList()
to take only 20 items from the list.
Take method allows you to set a limit on the items. It will only iterate the collection until the max count is reached. You can just use it instead of ToList()
or in case of _StaffNews
is defined as List<T>
, just combine them .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;
}
精彩评论