windows-phone-8 – LongListSelector第一个和最后一个项目的不
发布时间:2020-12-13 20:27:54 所属栏目:Windows 来源:网络整理
导读:我正在编写我的Windows Phone 8应用程序,它使用LongListSelector来显示一些数据. 如何为LongListSelector中的第一个和最后一个项设置不同的项模板? 基本上我只想在每个项目中显示相同的信息,但在最后和第一项中使用很少的不同项目“布局”. 您可以实现某种
我正在编写我的Windows Phone 8应用程序,它使用LongListSelector来显示一些数据.
如何为LongListSelector中的第一个和最后一个项设置不同的项模板? 基本上我只想在每个项目中显示相同的信息,但在最后和第一项中使用很少的不同项目“布局”.
您可以实现某种数据模板选择器,以帮助确定基于索引选择哪个模板.您可以从创建可重用的抽象TemplateSelector类开始.我使用了
Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector中解释的许多想法,但修改了实现以允许选择基于索引的模板.
public abstract class TemplateSelector : ContentControl { public abstract DataTemplate SelectTemplate(object item,int index,int totalCount,DependencyObject container); protected override void OnContentChanged(object oldContent,object newContent) { base.OnContentChanged(oldContent,newContent); var parent = GetParentByType<LongListSelector>(this); var index = parent.ItemsSource.IndexOf(newContent); var totalCount = parent.ItemsSource.Count; ContentTemplate = SelectTemplate(newContent,index,totalCount,this); } private static T GetParentByType<T>(DependencyObject element) where T : FrameworkElement { T result = null; DependencyObject parent = VisualTreeHelper.GetParent(element); while (parent != null) { result = parent as T; if (result != null) { return result; } parent = VisualTreeHelper.GetParent(parent); } return null; } } 拥有该类后,您可以添加自己的数据模板选择器逻辑.在你的情况下,可能是这样的 public class MyTemplateSelector : TemplateSelector { public DataTemplate First { get; set; } public DataTemplate Default { get; set; } public DataTemplate Last { get; set; } public override DataTemplate SelectTemplate(object item,DependencyObject container) { if (index == 0) return First; else if (index == totalCount-1) return Last; else return Default; } } 最后是Xaml <phone:PhoneApplicationPage.Resources> <DataTemplate x:Key="first"> <TextBlock Text="{Binding Name}" Foreground="Yellow" /> </DataTemplate> <DataTemplate x:Key="default"> <TextBlock Text="{Binding Name}" /> </DataTemplate> <DataTemplate x:Key="last"> <TextBlock Text="{Binding Name}" Foreground="Red" /> </DataTemplate> <DataTemplate x:Key="SelectingTemplate"> <local:MyTemplateSelector Content="{Binding}" First="{StaticResource first}" Default="{StaticResource default}" Last="{StaticResource last}" HorizontalContentAlignment="Stretch" /> </DataTemplate> </phone:PhoneApplicationPage.Resources> <phone:LongListSelector ItemTemplate="{StaticResource SelectingTemplate}" ItemsSource="{Binding Data}" /> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- vbscript – 如何使用Windows脚本以安全模式重新启动计算机
- 通过WPF样式或触发器设置TaskbarItemInfo
- Windows编译Nginx源码
- windows-server-2003 – 无法在适用于Windows的Amazon EC2实
- windows – 使用Powershell,比较两个事件的TimeCreated值,如
- windows-phone-7 – 如何从用户控件访问Windows Phone 7中的
- actionscript-3 – 如何在Windows 10下安装Flash调试版
- windows-phone-8.1 – 在Visual Studio 2013中建立Windows电
- windows下hexo+github搭建个人博客
- windows-server-2008 – 是否可以在没有活动目录的情况下设
推荐文章
站长推荐
- 如何在Windows XP中安装“GPT保护分区”?
- Windows Phone 8 – GetGeopositionAsync不返回
- portable-executable – Windows中.lib的格式是什
- windows-server-2003 – 服务器不支持客户端应用
- Signalr支持的平台
- windows-phone-8.1 – Windows Phone 8.1 – Bac
- windows-7 – 用于服务的Windows 7快捷方式和Win
- Microsoft SQL Server Like关键字
- haskell – 在Windows 7上安装带有cygwin的Cabal
- winforms – 为什么Windows窗体TextBox中的AutoS
热点阅读