I am trying to use an ObservableCollection of controls (ContentViews) in a CollectionView. The collection of ContentViews are currently being created and populated in the ctor of my ContentPage. I then set the ItemSource of the CollectionView to the property on the page that hold the ObservableCollection. My problem is that the ContentViews do not display at all.
Here is the XAML code from the ContentPage:
<ContentPage.Content>
<StackLayout>
<CollectionView ItemsSource="{Binding MyPolicies}">
<CollectionView.Header>
<StackLayout BackgroundColor="LightGray">
<Label Margin="10,0,0,0"
Text="My Policies"
FontSize="Small"
FontAttributes="Bold" />
</StackLayout>
</CollectionView.Header>
</CollectionView>
</StackLayout>
</ContentPage.Content>
Here is the code in the ContentPage constructor that creates ContentViews and adds them to the collection bound to the CollectionView :
public partial class TestCollectionView : ContentPage
{
public ObservableCollection<ThreeLineThreeColumnCardView> MyPolicies { get; private set; } = null;
public TestCollectionView()
{
IList<ThreeLineThreeColumnCardView> sourcePolicies = new List<ThreeLineThreeColumnCardView>();
CreateMyPolicies(sourcePolicies);
InitializeComponent();
}
private void CreateMyPolicies(IList<ThreeLineThreeColumnCardView> sourcePolicies)
{
//
sourcePolicies.Add(new ThreeLineThreeColumnCardView());
sourcePolicies.Add(new ThreeLineThreeColumnCardView());
sourcePolicies.Add(new ThreeLineThreeColumnCardView());
//
MyPolicies = new ObservableCollection<ThreeLineThreeColumnCardView>(sourcePolicies);
}
}
Here is the ContentView I'm using. You will notice that it has values set as defaults:
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="NGIC_XAML.Controls.CardViews.ThreeLineThreeColumnCardView">
<Frame WidthRequest="342"
BackgroundColor="#FFFFFF"
BorderColor="LightGray"
CornerRadius="5"
HasShadow="False"
Padding="8"
VerticalOptions="Center"
HorizontalOptions="Center">
<Grid WidthRequest="311" Margin="15, 13, 16, 13">
<Grid.RowDefinitions>
<RowDefinition Height="20" />
<RowDefinition Height="30" />
<RowDefinition Height="20" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0"
Text="{Binding CardTitle, FallbackValue='R0C0'}"
FontAttributes="None"
FontSize="14"
TextColor="{Binding CardTitleColor, FallbackValue='#333333'}"
VerticalTextAlignment="Center"
HorizontalTextAlignment="Start" />
<Label Grid.Row="0" Grid.Column="1"
Text="{Binding CardAmount, FallbackValue='R0C1'}"
FontAttributes="Bold"
FontSize="16"
TextColor="{Binding CardAmountColor, FallbackValue='#53585B'}"
VerticalTextAlignment="Center"
HorizontalTextAlignment="End" />
<Label Grid.Row="0" Grid.Column="2"
Text="{Binding CardAmount, FallbackValue='R0C2'}"
FontAttributes="Bold"
FontSize="16"
TextColor="{Binding CardAmountColor, FallbackValue='#53585B'}"
VerticalTextAlignment="Center"
HorizontalTextAlignment="End" />
<Label Grid.Row="1" Grid.Column="0"
Text="{Binding CardDate, FallbackValue='R1C0'}"
FontSize="12"
TextColor="{Binding CardDateColor, FallbackValue='#70777C'}"
VerticalTextAlignment="Center"
HorizontalTextAlignment="Start" />
<Label Grid.Row="1" Grid.Column="1"
Text="{Binding CardComment, FallbackValue='R1C1'}"
FontSize="12"
TextColor="{Binding CardCommentColor, FallbackValue='#70777C'}"
VerticalTextAlignment="Center"
HorizontalTextAlignment="End" />
<Label Grid.Row="1" Grid.Column="2"
Text="{Binding CardComment, FallbackValue='R1C2'}"
FontSize="12"
TextColor="{Binding CardCommentColor, FallbackValue='#70777C'}"
VerticalTextAlignment="Center"
HorizontalTextAlignment="End" />
<Label Grid.Row="2" Grid.Column="0"
Text="{Binding CardDate, FallbackValue='R2C0'}"
FontSize="12"
TextColor="{Binding CardDateColor, FallbackValue='#70777C'}"
VerticalTextAlignment="Center"
HorizontalTextAlignment="Start" />
<Label Grid.Row="2" Grid.Column="1"
Text="{Binding CardComment, FallbackValue='R2C1'}"
FontSize="12"
TextColor="{Binding CardCommentColor, FallbackValue='#70777C'}"
VerticalTextAlignment="Center"
HorizontalTextAlignment="End" />
<Label Grid.Row="2" Grid.Column="2"
Text="{Binding CardComment, FallbackValue='R2C2'}"
FontSize="12"
TextColor="{Binding CardCommentColor, FallbackValue='#70777C'}"
VerticalTextAlignment="Center"
HorizontalTextAlignment="End" />
</Grid>
</Frame>
</ContentView>
And this is what the ContentView looks like in its raw state:
So, my question is how do I get the collection of "cards" to display in the CollectionView on my ContentPage?
UPDATE Added to the ContentPage:
<DataTemplate>
<Grid Padding="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<cards:ThreeLineCardView />
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
UPADTE Demonstrate that the pre-populated card (a ContentView) can be displayed in the Grid outside of the CollectionView
<ContentPage.Content>
<StackLayout>
<!--<CollectionView ItemsSource="{Binding MyPolicies}">
<CollectionView.Header>
<StackLayout BackgroundColor="LightGray">
<Label Margin="10,0,0,0"
Text="My Policies"
FontSize="Small"
FontAttributes="Bold" />
</StackLayout>
</CollectionView.Header>
<CollectionView.ItemTemplate>
<DataTemplate>
<Grid Padding="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<cards:ThreeLineCardView />
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>-->
<Grid Padding="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<cards:ThreeLineCardView />
</Grid>
</StackLayout>
</ContentPage.Content>