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. php implode

    implode() 函数返回由数组元素组合成的字符串. <?php $arr = array('Hello','World!','I','love','Shanghai!'); echo imp ...

  2. 【转】oracle CONNECT BY PRIOR叶子节点查找根节点

    SELECT TRANS_ID FROM TRANS_INST WHERE connect_by_isleaf=1 START WITH TRANS_ID =480242 CONNECT BY PRI ...

  3. MyBatis 之 SqlSessionManager 源码分析

    MyBatis 的 4 个基本构成: SqlSessionFactoryBuilder(构造器): 根据配置信息或者代码来生成 SqlSessionFactory(工厂接口) SqlSessionFa ...

  4. 设计模式——代理模式(静态代理和JDK、CGLib动态代理)

    简介 什么是代理模式? 代理模式就是多一个代理类出来,代替原对象进行一些操作.比如说租房的中介.打官司的律师.旅行社,他们可以代替我们做一些事情,这就是代理. 代理模式的应用场景: 如果已有的方法在使 ...

  5. js 获取地址栏最后一个文件名称

    var JsRequest={ //这就是一个静态类,类里面有2个静态方法 //方法一:获取url的文件名 例如 index.html getUrlname:function(url){ //假如传进 ...

  6. 客户端通过HTTP协议与服务端交换数据

        客户端(包括浏览器)通过HTTP协议与服务端交换数据的描述 发起请求 header 键值对中的key大小写不敏感 Accept: application/json Content-Type: ...

  7. combobox无法显示选中的数据,都是undefined

    $('#firstfactor').combobox({                url: '@Url.Action("GetMultiAirFactor_Day_New", ...

  8. [Forward]Sweeping the IDisposable minefield

    IDisposable has been around since the beginning of .Net.The basic premise is simple.. Developers don ...

  9. 自然语言交流系统 phxnet团队 创新实训 项目博客 (十二)

    关于情感词典的学习总结: 情感倾向可认为是主体对某一客体主观存在的内心喜恶,内在评价的一种倾向.它由两个方面来衡量:一个情感倾向方向,一个是情感倾向度. 情感倾向方向也称为情感极性.在微博中,可以理解 ...

  10. 【转】C#多线程环境下调用 HttpWebRequest 并发连接限制

    .net 的 HttpWebRequest 或者 WebClient 在多线程情况下存在并发连接限制,这个限制在桌面操作系统如 windows xp , windows  7 下默认是2,在服务器操作 ...