Xamarin Forms ListView - Exception ?!?

thaufeethaufee Member ✭✭
edited April 25 in Xamarin.Forms

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

Best Answer

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Tick on exception settings for the at exception so Visual Studio stops at the error.
    Basic debugging. Probably nothing specific to do with Xamarin. You just have a bad cast someplace. Its a C# thing.

    Why do you have all these extra properties for your DateTime just to format a string? That's bulky and unneccesary. Just use StringFormat in your XAML. Let the UI layer deal with formatting; not your model.

    <ContentPage.Content>
    
    
    <ListView.HeaderTemplate>
    
    
    <Grid.RowDefinitions>
    
    </Grid.RowDefinitions>
    

    Looks like you're missing several XAML tags. You should clean up your post to properly markdown your code so the site parser doesn't try to render it.

  • thaufeethaufee Member ✭✭

    @ClintStLaurent said:
    Tick on exception settings for the at exception so Visual Studio stops at the error.
    Basic debugging. Probably nothing specific to do with Xamarin. You just have a bad cast someplace. Its a C# thing.

    Why do you have all these extra properties for your DateTime just to format a string? That's bulky and unneccesary. Just use StringFormat in your XAML. Let the UI layer deal with formatting; not your model.

    <ContentPage.Content>
    
    
    <ListView.HeaderTemplate>
    
    
    <Grid.RowDefinitions>
    
    </Grid.RowDefinitions>
    

    Looks like you're missing several XAML tags. You should clean up your post to properly markdown your code so the site parser doesn't try to render it.

    Sorry. It is a copy/paste mistake. Please see now, I have updated the XAML

  • thaufeethaufee Member ✭✭

    @ClintStLaurent said:

    System.ArgumentException: Value was an invalid value for HeaderTemplate

    Well yeah... You have a ViewCell in there.
    Header's don't have cells because they don't have repeating data like the body.
    You read the documentation on this before trying to use it, right?
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/listview/customizing-list-appearance

    Correct. It was the reason for this exception. I made a silly mistake.

Sign In or Register to comment.