Rendering elements in a CollectionView that is within a custom element

I’m currently trying to create a custom CollectionView element that has the first row frozen, so the user can see the first entry when scrolling down. After some trial and error, I concluded that the best way to do this was by rendering the first element separately from the rest. So I created a custom class as follows:

internal class CustomCollectionView : StackLayout
{
    public static readonly BindableProperty FirstItemProperty = BindableProperty.Create(
        nameof(FirstItem),
        typeof(string),
        typeof(CustomCollectionView),
        null);

    public string FirstItem
    {
        get => (string)GetValue(FirstItemProperty);
        set => SetValue(FirstItemProperty, value);
    }

    public static readonly BindableProperty RemainingItemsProperty = BindableProperty.Create(
        nameof(RemainingItems),
        typeof(ObservableCollection<Person>),
        typeof(CustomCollectionView),
        new ObservableCollection<Person>());
    
    public ObservableCollection<Person> RemainingItems
    {
        get => (ObservableCollection<Person>)GetValue(RemainingItemsProperty);
        set => SetValue(RemainingItemsProperty, value);
    }

    public CustomCollectionView()
    {
        var frozenRow = new Frame { 
            BackgroundColor = Colors.LightGray,
            Padding = new Thickness(10),
            Content = new Label { FontSize = 24, VerticalOptions = LayoutOptions.Center }
        };

        frozenRow.SetBinding(Label.TextProperty, new Binding(nameof(FirstItem), source: this.FirstItem));

        var collectionView = new CollectionView
        {
            ItemsSource = RemainingItems,
            ItemTemplate = new DataTemplate(() =>
            {
                return new Label { FontSize = 18, VerticalOptions = LayoutOptions.Center };
            })
        };

        collectionView.SetBinding(ItemsView.ItemsSourceProperty, new Binding(nameof(RemainingItems), source: this.RemainingItems));

        Children.Add(frozenRow);
        Children.Add(collectionView);
    }
}

In XAML, I tried to render this by:

<Grid>
    <local:CustomCollectionView FirstItem="{Binding HeaderExample}" RemainingItems="{Binding People}"></local:CustomCollectionView>
</Grid>

Where my ViewModel is defined as

class PersonViewModel
 {
     public ObservableCollection<Person> People { get; set; }
     public ObservableCollection<Person> HeaderItem { get; set; }

     public string HeaderExample; 

     public PersonViewModel()
     {
         // Sample data
         HeaderExample = "This is a header"; 
         HeaderItem = new ObservableCollection<Person> { new Person { Name = "Header", Age = 10, Location = "Korea" } };
         People = new ObservableCollection<Person>
     {
         new Person { Name = "Person A", Age = 30, Location = "Seoul" },
         new Person { Name = "Person B", Age = 31, Location = "Seoul" },
         new Person { Name = "Person B", Age = 31, Location = "Seoul" },
         new Person { Name = "Person B", Age = 31, Location = "Seoul" },
         new Person { Name = "Person B", Age = 31, Location = "Seoul" },
         new Person { Name = "Person B", Age = 31, Location = "Seoul" },
         new Person { Name = "Person B", Age = 31, Location = "Seoul" },
         new Person { Name = "Person B", Age = 31, Location = "Seoul" },
         new Person { Name = "Person B", Age = 31, Location = "Seoul" },
         new Person { Name = "Person B", Age = 31, Location = "Seoul" },
         new Person { Name = "Person B", Age = 31, Location = "Seoul" },
         new Person { Name = "Person B", Age = 31, Location = "Seoul" },
         new Person { Name = "Person B", Age = 31, Location = "Seoul" },
         new Person { Name = "Person B", Age = 31, Location = "Seoul" },
         new Person { Name = "Person B", Age = 31, Location = "Seoul" },
         new Person { Name = "Person B", Age = 31, Location = "Seoul" },
         new Person { Name = "Person B", Age = 31, Location = "Seoul" },
         new Person { Name = "Person B", Age = 31, Location = "Seoul" },
         new Person { Name = "Person B", Age = 31, Location = "Seoul" },
         new Person { Name = "Person B", Age = 31, Location = "Seoul" },
         new Person { Name = "Person B", Age = 31, Location = "Seoul" },
         new Person { Name = "Person B", Age = 31, Location = "Seoul" },
         new Person { Name = "Person B", Age = 31, Location = "Seoul" }
     };
     }
 }

However, this doesn’t seem to render properly and I’m not sure why. At this point, I have two questions:

  1. Is this even a good way to implement a frozen row in CollectionView? The only other working solution I was able to find is tinkering with the Translation.Y values when scrolling. This looks terrible though, as the row keeps moving and lagging behind the scroll, and I don’t intend to use it.

  2. How can I render the items properly? I’ve tried more basic renderings such as just trying to render the HeaderExample string,
    but it still does not work. Furthermore, I’ve confirmed that the items of my viewmodel render properly when using a regular CollectionView.

I would appreciate any help regarding this matter. Thank you.

6

You don’t need a Custom CollectionView element to keep FirstItem which is the first row frozen when scrolling down. You can just use a Grid and then put FirstItem in the first row and then put CollectionView below it which is similar as Jason suggested in above comment.

<Grid>     
      <Grid.ColumnDefinitions>
          <ColumnDefinition Width="1*"/>
      </Grid.ColumnDefinitions>

      <Grid.RowDefinitions>
          <RowDefinition Height="Auto" />
          <RowDefinition Height="Auto" />
      </Grid.RowDefinitions>

      <Grid ColumnSpacing="0" RowSpacing="0"  Grid.Row="0" Grid.Column="0"  >
          <Grid.RowDefinitions>
              <RowDefinition Height="Auto" />
          </Grid.RowDefinitions>

          <Entry BackgroundColor="Gray" Text="This ia a header"/>
      </Grid>

      <CollectionView  ItemsSource="{Binding People}" Grid.Row="1" Grid.Column="0" HeightRequest="800">
     
              <CollectionView.ItemTemplate>
                  <DataTemplate>
                      <Grid >
                          <Grid.ColumnDefinitions>
                             <ColumnDefinition Width="1*"/>
                              <ColumnDefinition Width="1*"/>
                              <ColumnDefinition Width="1*"/>
                          </Grid.ColumnDefinitions>

                          <Grid.RowDefinitions>
                              <RowDefinition Height="Auto" />
                          </Grid.RowDefinitions>

                          <Label Grid.Column="0" Text ="{Binding Name}" ></Label>
                          <Label Grid.Column="1"  Text ="{Binding Age}" ></Label>
                          <Label Grid.Column="2" Text ="{Binding Location}" ></Label>

                  </Grid>

                  </DataTemplate>
              </CollectionView.ItemTemplate>
          </CollectionView>   
</Grid>

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật