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. C++的虚函数试题,常考!!

    #include <iostream> #include <cstring> #include <string.h> #include <stdio.h> ...

  2. 定位被选中的select

    <script> var countryId = "{$user['country']}"; $("select[@name='country'] optio ...

  3. 通过USB连接越狱iPhone,SSH进入设备

    通过USB连接越狱iPhone,SSH进入设备html, body {overflow-x: initial !important;}.CodeMirror { height: auto; } .Co ...

  4. 编写js语句结束时保持良好的习惯-源于身边例子

    记录以下信息,源于一件事情,一位同事,每次我改他的js代码,发现语句结束都不使用分号作为结束.长长的一串,读起来比较吃力.即便语句的结束不使用分号结束,代码仍然不会报错,正常运行,所以不少程序员懒得去 ...

  5. 【Unity】协程Coroutine及Yield常见用法

    最近学习协程Coroutine,参考了别人的文章和视频教程,感觉协程用法还是相当灵活巧妙的,在此简单总结,方便自己以后回顾.Yield关键字的语意可以理解为“暂停”. 首先是yield return的 ...

  6. 编辑距离算法(Levenshtein)

    编辑距离定义: 编辑距离,又称Levenshtein距离,是指两个字串之间,由一个转成另一个所需的最少编辑操作次数. 许可的编辑操作包括:将一个字符替换成另一个字符,插入一个字符,删除一个字符. 例如 ...

  7. html超链接,锚点以及特殊字符

    超链接 <a></a>中不加东西是显示不了的. href:跳转的地址 target:_self(本页面打开,默认选项),_blank(新页面打开) title:文本提示 空链接 ...

  8. ELK(Logstash+Elasticsearch+Kibana)的原理和详细搭建

    一. Elastic Stack Elastic Stack是ELK的官方称呼,网址:https://www.elastic.co/cn/products ,其作用是“构建在开源基础之上, Elast ...

  9. 彻底去除Win10“快速访问”

    windows10的“快速访问”功能对于我个人用处不大,作为一个爱折腾的人决定尝试彻底除去“快速访问”这个侧边栏. 注意:此操作需要确保你已经设置了[让点击Win10任务栏“文件资源管理器”图标打开“ ...

  10. Ubuntu下的OpenResty 安装

    安装前的准备 您必须将这些库 perl 5.6.1+, libreadline, libpcre, libssl安装在您的电脑之中. 对于 Linux来说, 您需要确认使用 ldconfig 命令,让 ...