首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何格式化LongListSelector的样例设计时数据?

如何格式化LongListSelector的样例设计时数据?
EN

Stack Overflow用户
提问于 2012-03-12 00:40:19
回答 2查看 682关注 0票数 2

我想使用设计器(例如,表达式混合)来为LongListSelector控件(来自WP7的SL工具包)提供模板,但我不知道如何格式化数据以满足此控件的期望。

任何指向教程的指针都将不胜感激。谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-12 00:42:10

使用专门的集合,如下所示:

代码语言:javascript
运行
复制
using System;
using System.Linq;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Collections.ObjectModel;

namespace Stackoverflow.Collections
{
    /// <summary>
    /// Represents a specialized collection to integrate with the 
    /// <see cref="Microsoft.Phone.Controls.LongListSelector"/> control.
    /// </summary>
    /// <typeparam name="T">The type of the values in the collection.</typeparam>
    /// <typeparam name="TKey">The type of the keys in the collection.</typeparam>
    public class LongListCollection<T, TKey> : ObservableCollection<LongListItem<T, TKey>>
        where T : IComparable<T>
        where TKey : IComparable<TKey>
    {
        /// <summary>
        /// The key selector for adding items.
        /// </summary>
        private Func<T, TKey> keySelector;

        /// <summary>
        /// Initializes a new instance of the <see cref="LongListCollection&lt;T, TKey&gt;"/> class.
        /// </summary>
        /// <param name="keySelector">The key selector.</param>
        public LongListCollection(Func<T, TKey> keySelector)
        {
            if (keySelector == null)
                throw new ArgumentNullException("keySelector");

            this.keySelector = keySelector;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="LongListCollection&lt;T, TKey&gt;"/> class.
        /// </summary>
        /// <param name="keySelector">The key selector.</param>
        /// <param name="items">A initial collection.</param>
        public LongListCollection(Func<T, TKey> keySelector, IEnumerable<T> items)
        {
            if (keySelector == null)
                throw new ArgumentNullException("keySelector");

            if (items == null)
                throw new ArgumentNullException("items");

            this.keySelector = keySelector;

            var groups = new Dictionary<TKey, LongListItem<T, TKey>>();

            foreach (var item in items.OrderBy(x => x))
            {
                var key = keySelector(item);

                if (groups.ContainsKey(key) == false)
                    groups.Add(key, new LongListItem<T, TKey>(key));

                groups[key].Add(item);
            }

            foreach (var value in groups.Values)
                this.Add(value);
        }

        /// <summary>
        /// Adds the specified item to the collection.
        /// </summary>
        /// <param name="item">The item.</param>
        public void Add(T item)
        {
            TKey key = keySelector(item);

            var group = this.FirstOrDefault(x => x.Key.Equals(key));
            if (group != null)
                group.Add(item);                
            else
                this.Add(new LongListItem<T, TKey>(key) { item });
        }

        /// <summary>
        /// Inserts an item into the collection at the specified index.
        /// </summary>
        /// <param name="index">The zero-based index at which item should be inserted.</param>
        /// <param name="item">The object to insert.</param>
        protected override void InsertItem(int index, LongListItem<T, TKey> item)
        {
            for (int i = 0; i < this.Count; i++)
            {
                switch (Math.Sign(this[i].CompareTo(item)))
                {
                    case 0:
                        throw new InvalidOperationException("Cannot insert duplicated items.");
                    case 1:
                        base.InsertItem(i, item);
                        return;
                    case -1:
                        break;
                }
            }

            base.InsertItem(this.Count, item);
        }
    }

    /// <summary>
    /// Represents a specialized data structure to integrate with the 
    /// <see cref="Microsoft.Phone.Controls.LongListSelector"/> control.
    /// </summary>
    /// <typeparam name="T">The type of the values in the structure.</typeparam>
    /// <typeparam name="TKey">The type of the key in the structure.</typeparam>
    public class LongListItem<T, TKey> : ObservableCollection<T>, IComparable<LongListItem<T, TKey>>
        where T : IComparable<T>
        where TKey : IComparable<TKey>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="LongListItem&lt;T, TKey&gt;"/> class.
        /// </summary>
        public LongListItem()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="LongListItem&lt;T, TKey&gt;"/> class.
        /// </summary>
        /// <param name="key">The item's key.</param>
        public LongListItem(TKey key)
        {
            this.Key = key;
        }

        /// <summary>
        /// Gets or sets the item key.
        /// </summary>
        /// <value>The item key.</value>
        public TKey Key
        {
            get;
            set;
        }

        /// <summary>
        /// Gets a value indicating whether this instance has any items.
        /// </summary>
        /// <value><c>true</c> if this instance has any items; otherwise, <c>false</c>.</value>
        public bool HasItems
        {
            get
            {
                return Count > 0;
            }
        }

        /// <summary>
        /// Inserts an item into the collection at the specified index.
        /// </summary>
        /// <param name="index">The zero-based index at which item should be inserted.</param>
        /// <param name="item">The object to insert.</param>
        protected override void InsertItem(int index, T item)
        {                              
            for (int i = 0; i < this.Count; i++)
            {
                switch (Math.Sign(this[i].CompareTo(item)))
                {
                    case 0:
                        return;
                    case 1:
                        base.InsertItem(i, item);
                        return;
                    case -1:
                        break;
                }
            }

            base.InsertItem(this.Count, item);
        }

        /// <summary>
        /// Compares to.
        /// </summary>
        /// <param name="other">The other.</param>
        /// <returns></returns>
        public int CompareTo(LongListItem<T, TKey> other)
        {
            if (other == null)
                return 1;

            return this.Key.CompareTo(other.Key);
        }
    }
}
票数 0
EN

Stack Overflow用户

发布于 2013-02-05 04:19:00

我在博客上写了一个分步指南;http://pauliom.com/2013/02/03/how-to-use-blend-sample-data-with-a-longlistselector/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9656809

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档