简体   繁体   中英

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()
                        {                   
                            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;            
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM