如何使用ListViewRenderer
问题描述:
我在App.xaml
定义了以下HeaderTemplate
使用在Xamarin UWP一个ListView的页眉模板:如何使用ListViewRenderer
<DataTemplate x:Key="ListViewGroupHeaderTemplate">
<TextBlock Foreground="Black" Text="Something" />
</DataTemplate>
我使用这种方式在我ListViewRenderer
:
listView.ItemTemplate = App.Current.Resources["ListViewItemTemplate"] as Windows.UI.Xaml.DataTemplate;
var gs = new GroupStyle();
gs.HeaderTemplate = App.Current.Resources["ListViewGroupHeaderTemplate"] as Windows.UI.Xaml.DataTemplate;
listView.GroupStyle.Add(gs);
源我的ListView
是CollectionViewSource
:
CollectionViewSource cvs = new CollectionViewSource();
cvs.IsSourceGrouped = true;
cvs.Source = ((SourceListView)Element).Items;
if (listView.ItemsSource == null)
listView.ItemsSource = cvs.View;
ItemTemplate
效果很好。
这是我的长相ListView
如何(白色部分是组头):
我应该怎么设置使头的工作?
UPDATE:
我根据答案修改了源(应用于分组到一个简单的列表):
CollectionViewSource cvs = new CollectionViewSource();
cvs.IsSourceGrouped = true;
var items = ((MyListView)Element).Items;
var itemsAsOC = items as ObservableCollection<GroupListItem>;
List<ListItem> list = new List<ListItem>();
foreach (var item in itemsAsOC)
{
list.AddRange(item);
}
var group = from item in list group item by item.Value into grp orderby grp.Key select grp;
cvs.Source = group;
没有与头文件改变了,所以我想数据源是正确的但有一些严重的Xamarin UWP错误模板处理:
更新2:
我测试而不Xamarin渲染,它显示了标题项目。
然而,当我使用Xamarin渲染,头都不见了。
您可以download和动作进行测试。
答
基本上我在代码中找不到任何明确的问题,下面是我如何将GroupHeader
模板添加到ListView
。
In App。XAML:
<Application.Resources>
<DataTemplate x:Key="ListViewGroupHeaderTemplate">
<TextBlock Foreground="Red" Text="{Binding Key}" />
</DataTemplate>
<DataTemplate x:Key="ListViewTemplate">
<TextBlock Foreground="Black" Text="{Binding DateTimeProperty}" />
</DataTemplate>
</Application.Resources>
这里是我的物品类别和我SourceListView
:
public class ListItem
{
public DateTime DateTimeProperty { get; set; }
}
public class SourceListView
{
public SourceListView()
{
Element = from x in Collection group x by x.DateTimeProperty.Year into grp orderby grp.Key select grp;
}
public IList<ListItem> Collection { get; set; } = new List<ListItem>()
{
new ListItem() {DateTimeProperty = DateTime.Parse("2011-08-01") },
new ListItem() {DateTimeProperty = DateTime.Parse("2011-08-21") },
new ListItem() {DateTimeProperty = DateTime.Parse("2012-04-03") },
new ListItem() {DateTimeProperty = DateTime.Parse("2012-04-01") },
new ListItem() {DateTimeProperty = DateTime.Parse("2013-05-15") },
new ListItem() {DateTimeProperty = DateTime.Parse("2013-05-06") },
new ListItem() {DateTimeProperty = DateTime.Parse("2013-05-01") },
new ListItem() {DateTimeProperty = DateTime.Parse("2013-06-01") },
new ListItem() {DateTimeProperty = DateTime.Parse("2013-06-09") },
new ListItem() {DateTimeProperty = DateTime.Parse("2014-10-02") },
new ListItem() {DateTimeProperty = DateTime.Parse("2014-10-11") },
new ListItem() {DateTimeProperty = DateTime.Parse("2015-12-03") },
new ListItem() {DateTimeProperty = DateTime.Parse("2016-07-01") },
new ListItem() {DateTimeProperty = DateTime.Parse("2016-07-06") },
};
public IEnumerable<object> Element { get; }
}
下面是实现这一点的代码分组ListView
:
ListView listView = new ListView();
listView.ItemTemplate = App.Current.Resources["ListViewTemplate"] as Windows.UI.Xaml.DataTemplate;
var gs = new GroupStyle();
gs.HeaderTemplate = App.Current.Resources["ListViewGroupHeaderTemplate"] as Windows.UI.Xaml.DataTemplate;
listView.GroupStyle.Add(gs);
CollectionViewSource cvs = new CollectionViewSource();
cvs.IsSourceGrouped = true;
cvs.Source = new SourceListView().Element;
if (listView.ItemsSource == null)
listView.ItemsSource = cvs.View;
rootGrid.Children.Add(listView);
这里是绘制图像:
答
好吧,终于在经历了一天半的苦苦挣扎之后,我找到了解决办法。如果某个人有一天面临与我一样的问题,我会在这里发布。其实这是很奇怪的问题,我不知道这是否是一个错误,未实现的功能还是什么......
解决办法:
在你UWP自定义呈现你需要这样做:
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.ListView> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
e.NewElement.GroupHeaderTemplate = new Xamarin.Forms.DataTemplate(() =>
{
var label = new Label();
label.SetBinding(Label.TextProperty, "Country");
return new ViewCell { View = label };
});
}
}
其中“Country”是表示您的Group/Header的类的属性名称。
如果我添加如下代码:˛'listView.HeaderTemplate= App.Current.Resources [“ListViewGroupHeaderTemplate”] as Windows.UI.Xaml.DataTemplate;',那么主标题出现,所以我猜,组标题设置不正确。我应该在我的CollectionViewSource中设置_a key_ value_吗? – Nestor
@Nestor,如果你将'listView.HeaderTemplate = App.Current.Resources [“ListViewGroupHeaderTemplate”]设置为Windows.UI.Xaml.DataTemplate;',那么这个头文件就是这个ListView的主头文件,你的群组将无法构建通过项目是为了,但每个组的头应该是缺少......“我应该在我的CollectionViewSource中设置一个键值”,键值是为组标题,它使用LinQ进行分组,分组时,他们是关键 - 值对,所以我使用'Key'来绑定ListViewGroupHeaderTemplate中的'CollectionViewSource',你只需要添加这个模板。 –
我修改了我的模板,_fix title_被替换为{{Binding Key}},但没有任何改变。另外,我想一个修复标题应该是可见的,因为它不依赖任何绑定。我在我的XAML中添加了'GroupStyle',但头部仍然是一个白色的矩形。很奇怪,阻碍了我的工作。 – Nestor