Mohamed Thaufeeq
Mohamed Thaufeeq

Reputation: 1677

Xamarin Forms ListView - Exception?

I have a very simple ListView that was programmed to show a list that has three properties. Whenever I try to bind a collection to that listview, I get InvalidCast Exception. I don't know what is wrong in that!

Below is my XAML

<ContentPage.Content>
    <StackLayout>
        <ListView x:Name="listAttendanceTiming" HasUnevenRows="True">
            <ListView.HeaderTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>

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

                            <Label Text="From Time" HorizontalTextAlignment="Start" FontSize="14"
                                   Grid.Row="0" Grid.Column="0" />

                            <Label Text="To Time" HorizontalTextAlignment="Start" FontSize="14"
                                   Grid.Row="0" Grid.Column="1" />

                            <Label Text="Remarks" HorizontalTextAlignment="Start" FontSize="14"
                                   Grid.Row="0" Grid.Column="2" />

                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.HeaderTemplate>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>

                            <Label Text="{Binding FromTimeString}" HorizontalTextAlignment="Start" FontSize="14" Grid.Column="0" />
                            <Label Text="{Binding ToTimeString}" HorizontalTextAlignment="Start" FontSize="14" Grid.Column="1" />
                            <Label Text="{Binding Remarks}" HorizontalTextAlignment="Start" FontSize="14" Grid.Column="2" />

                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage.Content>

And here is my C#

var timings = new List<Timing>();

var timing = new Timing();
timing.Id = 1;
timing.FromTime = DateTime.Now;
timing.FromTimeString = DateTime.Now.ToString("hh:mm ttBel");
timing.ToTime = DateTime.Now;
timing.ToTimeString = DateTime.Now.ToString("hh:mm tt");
timing.Code = "I";
timing.Remarks = "Inside";
timings.Add(timing);
listAttendanceTiming.ItemsSource = timings;

public class Timing
{
    public int Id { get; set; }
    public DateTime FromTime { get; set; }
    public string FromTimeString { get; set; }
    public DateTime ToTime { get; set; }
    public string ToTimeString { get; set; }
    public string Code { get; set; }
    public string Remarks { get; set; }
}

Exception

Unhandled Exception:

System.ArgumentException: Value was an invalid value for HeaderTemplate Parameter name: value occurred

Upvotes: 3

Views: 693

Answers (2)

Mohamed Thaufeeq
Mohamed Thaufeeq

Reputation: 1677

ListView.HeaderTemplate.DataTemplate does not require ViewCell property. Changing ListView.HeaderTemplate like below will resolve this issue.

<ListView.HeaderTemplate>
    <DataTemplate>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

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

            <Label Text="From Time" HorizontalTextAlignment="Start" FontSize="14"
                   Grid.Row="0" Grid.Column="0" />

            <Label Text="To Time" HorizontalTextAlignment="Start" FontSize="14"
                   Grid.Row="0" Grid.Column="1" />

            <Label Text="Remarks" HorizontalTextAlignment="Start" FontSize="14"
                   Grid.Row="0" Grid.Column="2" />

        </Grid>
    </DataTemplate>
</ListView.HeaderTemplate>

Upvotes: 7

Sven-Michael St&#252;be
Sven-Michael St&#252;be

Reputation: 14750

I think you are missing

<DataTemplate>
    <Grid>

after <ListView.HeaderTemplate>

Please post the complete exception the next time.

Upvotes: 1

Related Questions