DataTemplate中的CollectionView始终为空 [英] CollectionView within a DataTemplate always remains empty

查看:65
本文介绍了DataTemplate中的CollectionView始终为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 CarouselView ,其中在 DataTemplate 内有一个 CollectionView ,其 ItemsSource 属性已绑定到 Observablecollection ,其中仅当用户按下按钮时才添加元素.问题是此 CollectionView 始终为空,我看不到任何项目

I have a CarouselView in which inside the DataTemplate I have a CollectionView, whose ItemsSource property is binded to an Observablecollection, in which elements are added only when the user presses on a button. The problem is that this CollectionView is always empty, I can't see any items

<CarouselView
                    x:Name="CollectionDiary"
                    RelativeLayout.YConstraint="{ConstraintExpression
                    Type=Constant,
                    Constant=242}"
                    RelativeLayout.WidthConstraint="{ConstraintExpression
                    Type=RelativeToParent,
                    Property=Width,
                    Factor=1}"
                    PeekAreaInsets="20"
                    HeightRequest="330">
                    <CarouselView.ItemsLayout>
                        <LinearItemsLayout Orientation="Horizontal" SnapPointsType="MandatorySingle" SnapPointsAlignment="Center" ItemSpacing="5"/>
                    </CarouselView.ItemsLayout>
                    <CarouselView.ItemTemplate>
                        <DataTemplate>
                            <yummy:PancakeView  CornerRadius="22" BackgroundColor="Transparent">
                                <yummy:PancakeView.Shadow>
                                    <yummy:DropShadow Color="#000000" Offset="2,2" BlurRadius="5" Opacity="0.8" />
                                </yummy:PancakeView.Shadow>
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="25"/>
                                        <RowDefinition Height="15"/>
                                        <RowDefinition Height="45"/>
                                        <RowDefinition Height="268"/>
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="70"/>
                                        <ColumnDefinition/>
                                    </Grid.ColumnDefinitions>
                                    <Grid.Background>
                                        <LinearGradientBrush StartPoint="1,0" EndPoint="1,1">
                                                <GradientStop Color="Black" Offset="0" />
                                                <GradientStop Color="#4191cc" Offset="1.0" />
                                        </LinearGradientBrush>
                                    </Grid.Background>
                                    <Label Grid.Row="0" Grid.Column="1" Text="{Binding Data}" FontSize="14" TextColor="Gray" VerticalTextAlignment="End" Margin="10,5,10,0"/>
                                    <Label Grid.Row="1" Grid.Column="1" Text="{Binding Orario}" FontSize="14" TextColor="Gray" VerticalTextAlignment="End" Margin="10,0"/>
                                    <Image Grid.Row="0" Grid.RowSpan="3" Grid.Column="0" Margin="15,15,0,0" Source="{Binding Umore}" HeightRequest="60"/>
                                    <Label Grid.Row="2" Grid.Column="1" Text="{Binding TipoUmore}" FontAttributes="Bold" TextColor="{Binding ColoreUmore}" FontSize="22" Margin="10,0" VerticalOptions="Start"/>
                                    <Label Grid.Row="3" Grid.Column="1" TextColor="White" FontSize="16" Text="{Binding Nota}" Margin="10,0" VerticalOptions="Start" HorizontalTextAlignment="Start"/>
                                    <CollectionView
                                        Grid.Row="2"
                                        Grid.Column="0"
                                        Margin="10"             
                                        SelectionMode="None"
                                        ItemsSource="{Binding listEmojiDiarioView}">
                                        <CollectionView.ItemsLayout>
                                            <GridItemsLayout Orientation="Vertical" VerticalItemSpacing="10" HorizontalItemSpacing="10"/>
                                        </CollectionView.ItemsLayout>
                                        <CollectionView.ItemTemplate>
                                            <DataTemplate>
                                                <Grid>
                                                    <Grid.RowDefinitions>
                                                        <RowDefinition Height="*"/>
                                                    </Grid.RowDefinitions>
                                                    <Grid.ColumnDefinitions>
                                                        <ColumnDefinition Width="*"/>
                                                        <ColumnDefinition Width="Auto"/>
                                                    </Grid.ColumnDefinitions>
                                                    <Image Grid.Row="0" Grid.Column="0" Source="{Binding SourceImg}"/>
                                                    <Label Grid.Row="0" Grid.Column="1" Text="{Binding id}" TextColor="White" FontSize="13"/>
                                                </Grid>
                                            </DataTemplate>
                                        </CollectionView.ItemTemplate>
                                    </CollectionView>
                                </Grid>
                            </yummy:PancakeView>
                        </DataTemplate>
                    </CarouselView.ItemTemplate>
                </CarouselView>

c#

ObservableCollection<HumorDiary> listDiario = new ObservableCollection<HumorDiary>();
ObservableCollection<IconDiary> listEmojiDiarioView = new ObservableCollection<IconDiary>();

        private async void BTSaveDiary_Clicked(object sender, EventArgs e)
                {
    
                     HumorDiary hum = new HumorDiary
                    {
                        Nota = TestoDiary.Text,
                        Data = DateTime.Today.Day.ToString() + " " + Mese,
                        Orario = DateTime.Now.TimeOfDay.ToString().Remove(5,DateTime.Now.TimeOfDay.ToString().Length-5),
                        Anno = DateTime.Now.Year.ToString(),
                        Umore = "Smile" + NUmore + ".png",
                        TipoUmore = tipoUmore,
                        ColoreUmore = Colore,
                        IconTot = NIcon,
                    };
    
                    string[] srListIcon = NIcon.Split(new string[] { set.Tag }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string sr in srListIcon)
                    {
                        string src = sr.Substring(0, 9);
                        string id = sr.Substring(9, sr.Length-9);
                        IconDiary ic = new IconDiary
                        {
                            SourceImg = src,
                            id = id,
                        };
                        listEmojiDiarioView.Add(ic);
                    }
    
                    listDiario.Insert(0, hum);
                    CollectionDiary.ItemsSource = listDiario.Take(3);
             }

推荐答案

  1. 由于 CarouselView 中的 BindingContext 发生了变化,并将其 ItemsSource 用作 BindingContext ,因此您需要显式如果您的 CollectionView ,请指定您的 BindingContext .我想如果您查看日志,会发现一些绑定错误/失败,因为它正在寻找 listDiario 中名称为 listEmojiDiarioView IEnumerable 属性.code>显然不存在.
  1. Since the BindingContext inside a CarouselView changes and takes it ItemsSource as a BindingContext, you need to explicitly specify your BindingContext in case of your CollectionView. I guess if you look at the logs you will find some binding error/failure because it was looking for an IEnumerable property with the name of listEmojiDiarioView inside listDiario which obviously is not there.

克服这一问题的一种方法是使用页面引用作为绑定源:

One way you can overcome this is by using a reference of your page as binding source:

<ContentPage x:Name="pageref" ...

     <CollectionView ...
          ItemsSource="{Binding Source={x:Reference pageref}, Path=BindingContext.listEmojiDiarioView}">

  1. listEmojiDiarioView 必须是属性而非字段

如果天气不好,请检查是否已在绑定或否的 ObservableCollection 上实现了 INotifyPropertyChanged .省略的代码保持不变.

If it is not working check weather you have implemented the INotifyPropertyChanged on your ObservableCollection used in binding or no. Omitted code remains unchanged.

相关文档

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/xaml/xaml-basics/data-binding-basics

这篇关于DataTemplate中的CollectionView始终为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆