WP8的WindowsPhoneToolkit工具包中有一个 LoopingSelector

可以想选择日期或时间一样进行选择

1、首先当然是引用WindowsPhoneToolkit

  在Nuget控制台:

 PM> Install-Package WPtoolkit

2、LoopingSelector 的数据源是 ILoopingSelectorDataSource类型的,我们先实现两个类继承该接口

    public abstract class LoopingDataSourceBase : ILoopingSelectorDataSource
{
#region ILoopingSelectorDataSource Members public abstract object GetNext(object relativeTo); public abstract object GetPrevious(object relativeTo); private object _selectedItem;
public object SelectedItem
{
get { return _selectedItem; }
set
{
if (!Equals(_selectedItem, value))
{
object previousSelectedItem = _selectedItem;
_selectedItem = value; OnSelectionChanged(previousSelectedItem, _selectedItem);
}
}
} public event EventHandler<SelectionChangedEventArgs> SelectionChanged; protected virtual void OnSelectionChanged(object oldSelectedItem, object newSelectedItem)
{
var handler = SelectionChanged;
if (handler != null)
{
handler(this, new SelectionChangedEventArgs(new[] {oldSelectedItem}, new[] {newSelectedItem}));
}
} #endregion
}

LoopingDataSourceBase 抽象类

    public class ListLoopingDataSource<T> : LoopingDataSourceBase
{
private IComparer<T> comparer;
private LinkedList<T> linkedList;
private NodeComparer nodeComparer;
private List<LinkedListNode<T>> sortedList; public IEnumerable<T> Items
{
get { return linkedList; }
set
{
SetItemCollection(value);
}
} public IComparer<T> Comparer
{
get { return comparer; }
set { comparer = value; }
} private void SetItemCollection(IEnumerable<T> collection)
{
linkedList = new LinkedList<T>(collection); sortedList = new List<LinkedListNode<T>>(linkedList.Count); LinkedListNode<T> currentNode = linkedList.First;
while (currentNode != null)
{
sortedList.Add(currentNode);
currentNode = currentNode.Next;
} IComparer<T> comparer = this.comparer;
if (comparer == null)
{
if (typeof (IComparable<T>).IsAssignableFrom(typeof (T)))
{
comparer = Comparer<T>.Default;
}
else
{
throw new InvalidOperationException(
"There is no default comparer for this type of item. You must set one.");
}
} nodeComparer = new NodeComparer(comparer);
sortedList.Sort(nodeComparer);
} public override object GetNext(object relativeTo)
{
int index = sortedList.BinarySearch(new LinkedListNode<T>((T) relativeTo), nodeComparer);
if (index < )
{
return default(T);
} LinkedListNode<T> node = sortedList[index].Next;
if (node == null)
{
node = linkedList.First;
}
return node.Value;
} public override object GetPrevious(object relativeTo)
{
int index = sortedList.BinarySearch(new LinkedListNode<T>((T) relativeTo), nodeComparer);
if (index < )
{
return default(T);
}
LinkedListNode<T> node = sortedList[index].Previous;
if (node == null)
{
node = linkedList.Last;
}
return node.Value;
} private class NodeComparer : IComparer<LinkedListNode<T>>
{
private readonly IComparer<T> comparer; public NodeComparer(IComparer<T> comparer)
{
this.comparer = comparer;
} #region IComparer<LinkedListNode<T>> Members public int Compare(LinkedListNode<T> x, LinkedListNode<T> y)
{
return comparer.Compare(x.Value, y.Value);
} #endregion
}
}

ListLoopingDataSource

  注意,数据源如果是对象,必须实现IComparer<T>接口,否则会抛出异常,当然,也可以重写ListLoopingDataSource类

下面是数据源对象,我们这里定义为Person

    public class Person : IComparable<Person>
{
public string Name { get; set; }
public int Age { get; set; } public int CompareTo(Person other)
{
//比较两个对象:这里只比较名字
return String.CompareOrdinal(Name, other.Name);
}
}

Person

3、接下来是数据绑定

            <toolkitPrimitives:LoopingSelector
x:Name="LoopingSelector"
DataSource="{Binding Items}"
ItemMargin="2,3,3,2"
ItemSize="200,150"
FontSize="33" >
<toolkitPrimitives:LoopingSelector.ItemTemplate>
<DataTemplate>
<Grid>
<StackPanel>
<TextBlock Text="{Binding Name}"></TextBlock>
<TextBlock Text="{Binding Age}"></TextBlock>
</StackPanel>
</Grid>
</DataTemplate>
</toolkitPrimitives:LoopingSelector.ItemTemplate>
</toolkitPrimitives:LoopingSelector>
    public partial class LoopSelectorPage : INotifyPropertyChanged
{
#region Items /// <summary>
/// The <see cref="Items" /> property's name.
/// </summary>
public const string ItemsPropertyName = "Items"; private ListLoopingDataSource<Person> _items; /// <summary>
/// 用于绑定到LoopingSelector的数据源对象
/// </summary>
public ListLoopingDataSource<Person> Items
{
get
{
return _items;
} set
{
if (_items == value)
{
return;
} _items = value;
RaisePropertyChanged(ItemsPropertyName);
}
} #endregion public LoopSelectorPage()
{
InitializeComponent();
LoadApplicationBar();
LoadData();
} private void LoadApplicationBar()
{
ApplicationBar = new ApplicationBar(); var appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative))
{
Text = "test"
};
appBarButton.Click += appBarButton_Click;
ApplicationBar.Buttons.Add(appBarButton); } private void appBarButton_Click(object sender, EventArgs e)
{
//改变数据源
Items = new ListLoopingDataSource<Person>
{
Items = new ObservableCollection<Person>
{
new Person{Name = "Cnblogs", Age = },
new Person{Name = "CodePlex", Age = },
new Person{Name = "CodeProject", Age = },
new Person{Name = "CSDN", Age = },
new Person{Name = "51CTO", Age = },
},
};
//注意,如果改变了数据源,必须设置其SelectedItem属性
Items.SelectedItem = Items.Items.First();
} private void LoadData()
{
Items = new ListLoopingDataSource<Person>
{
Items = new ObservableCollection<Person>
{
new Person{Name = "aaa", Age = },
new Person{Name = "bbb", Age = },
new Person{Name = "ccc", Age = },
new Person{Name = "ddd", Age = },
new Person{Name = "eee", Age = },
},
}; //注意,如果改变了数据源,必须设置其SelectedItem属性
Items.SelectedItem = Items.Items.First(); //也可以监听选择改变的事件
Items.SelectionChanged += Items_SelectionChanged; } void Items_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
MessageBox.Show(string.Format("add:{0}", ((Person)e.AddedItems[]).Name));
} #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; protected void RaisePropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
} #endregion
}

LoopSelectorPage.xaml.cs

好了,数据源的集合是用 IEnumerable<T>存放的,如果需要添加和删除该数据源,可以使用List<T>对象,或是ObservableCollection<T> 对象存储

【WP8】LoopingSelector的更多相关文章

  1. 【WP8】WebBrowser相关

    2014年09月02日更新 今天用了一下WebBrowser,在使用过程中也遇到了一些问题,在这里做一下记录 虽然WebBrowser比较重,会比较影响性能(除非一定要用到它,否则尽量少用),但有时候 ...

  2. 【WP8】线程安全的StorageHelper

    14-08-29 12:32更新:修复StorageHelper部分bug WP8以后提供了StorageFile的方式访问文件,StorageFile对文件的操作只提供了异步的支持,包括WP8.1 ...

  3. 【WP8】让TextBox文本支持滑动(Scroll)

    通过修改样式让TextBox支持文本滑动 在Silverlight上,TextBox是有文本滚动的功能的,当TextBox的文本过长时,可以进行拖动的,TextBox使用 VerticalScroll ...

  4. 【WP8】自定义配置存储类

    之前在WP7升级到WP8的时候遇到配置不兼容的问题 情景:之前只有一个WP7版本,现在需要发布WP8版本,让用户可以从原来的WP7版本升级到WP8版本 一般情况下从WP7升级到WP8没什么问题 但是在 ...

  5. 【WP8】ResourceDictionary

    WP8中引用资源字典 当我们定义的样式太多的时候,我们可以把样式分别定义在不同的文件中,然后通过 MergedDictionaries 应用到其他资源字典中,看下面Demo 我们可以把样式定义在多个文 ...

  6. 【WP8】为Webbrowser添加ScrollBar

    在WP8中,控件WebBrowser没有提供对滚动条的支持,而在内置的IE中,却可以显示出滚动条(微软这是在坑我们吗),但如果在客户端使用Webbrowser的话,自己构造ScrollBar来显示 通 ...

  7. 【WP8】Uri关联启动第三方App

    在WP8中支持启动第三方应用程序,比如在App1中可以打开App2,你可以在你的应用程序中直接打开QQ,也可以让其他开发者调用你的APP,例如:软件盒子 下面演示被调用方和调用方的使用方法,新建两个项 ...

  8. 【WP8】WP8调用官方API使用LED灯

    在WP7中没有相关的API可以直接使用摄像头的LED等,只能通过录像时打开LED等来使用,在WP8中添加了相关的调用接口,可以方便的使用LED灯,并且支持后台,废话不多说,直接上代码 1.在 WMAp ...

  9. 【WP8】扩展CM的WindowManager

    14-09-09更新:修复AppBar冲突bug 关于WindowManager,一直都很想写一篇博客分享一下,一直在忙别的,今天抽空把这个分享一下 在弹窗在移动开发是一个很常见的交互,很多时候我们都 ...

随机推荐

  1. java基础篇---Servlet监听器

    在写此篇前,看了一下园友写的,感觉其基础知识归纳的十分全面,我在此就不累赘的写了,链接地址(http://www.cnblogs.com/sherryueda/p/4273169.html), 我就写 ...

  2. Android ListView的使用(一)

    初次接触listview,以为直接在Android Studio 中将控件给拖过去,就能够使用了,结果半天显示不了. 后来总算知道原因了. 先上代码: activity_main.xml 显示页面 里 ...

  3. [转]Oracle 语法之 OVER (PARTITION BY ..) 及开窗函数

    oracle的分析函数over 及开窗函数 一:分析函数Oracle从8.1.6开始提供分析函数,分析函数用于计算基于组的某种聚合值,它和聚合函数的不同之处是 对于每个组返回多行,而聚合函数对于每个组 ...

  4. HashTable浅析

    本文转载自: http://rock3.info/blog/2013/12/05/hashtable%E6%B5%85%E6%9E%90/ 一.Hash特点 Hash,就是杂凑算法,Hash(str1 ...

  5. plot sin 动态配置rc settings

    plot sin 动态配置rc settings 坐标轴颜色 线的颜色 绘图前景色 Code #!/usr/bin/env python # -*- coding: utf-8 -*- import ...

  6. Html模板渲染引擎Hogan

    Github:https://github.com/twitter/hogan.js 最简单的使用教程:http://www.imooc.com/article/18493

  7. mypy 支持静态类型编程的python变种

    每种编程语言都有一群固定的用户,对于那些习惯将不同编程语言用成同样的感觉的人来说,最是难受.因为每种语言都有它独特的设计『哲学』和擅长的应用领域. PHP给大家的一贯的印象都是动态弱类型语言,Pyth ...

  8. [Python学习]Python学习资料汇总

    Python其实很简单,入门+提高看一些简单的教程就能学会,这里列出一些比较好的资料: 廖雪峰博客 https://www.liaoxuefeng.com/wiki/0014316089557264a ...

  9. java中GZIPOutputStream 流的使用(EOFException)

    GZip流的使用非常多人都出现了以下的异常:java.io.EOFException: Unexpected end of ZLIB input stream.或者出现压缩后的数据不全的情况(就是压缩 ...

  10. python出现UnicodeEncodeError有可能产生的另一个原因

    在使用python中,我们都有可能遇到如下的错误: UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ...