Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Setting ItemsSource for ListView defined in XAML throws InvalidCastException at runtime

I am seeing some strange behavior with the ListView control when I create the ListView in Xaml

If I define a ListView in Xaml

            <ListView x:Name="listView" VerticalOptions="FillAndExpand">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <Label Text="{Binding Title}" />
                        <!--<ImageCell Text="{Binding Title}" ImageSource="{Binding IconSource}" />-->
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

and then in code set the ItemsSource

         var masterPageItems = MasterPageItems = new ObservableCollection<MasterPageItem>()
        {
            new MasterPageItem
            {
                Title = "Map",
                TargetType = typeof (MapPage)
            },
            new MasterPageItem
            {
                Title = "Start Survey",
                TargetType = typeof (StartPage)
            },
            new MasterPageItem
            {
                Title = "About",
                TargetType = typeof (AboutPage)
            }
        };  

        listView.ItemsSource = masterPageItems; 

When I run the application crashes and I will get: a message box System.InvalidCastException: Specified cast is not valid.

However if I create the ListView in code it works just fine.

           ListView list = new ListView
            {
                ItemsSource = masterPageItems
            };

Any ideas what is going on here? I would much rather be doing this in Xaml

Thanks
-Joe

Posts

  • JoeH.6170JoeH.6170 USMember

    Thank you David, that resolves my problem

  • Oracle22Oracle22 USMember ✭✭
    edited July 2017

    Thank you David, that resolves my problem. I Had forget the ViewCell after DataTemplate

                    <ListView  ItemsSource="{Binding Items}" ItemTapped="Handle_ItemTapped" CachingStrategy="RecycleElement" HorizontalOptions="StartAndExpand" SeparatorVisibility="None" WidthRequest="100" Margin="5">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell>
                                    <ViewCell.View>
                                        <StackLayout HeightRequest="100">
                                            <nanoXam:CircleButton HeightRequest="100" Text="{Binding Label}" ButtonTextColor="White" ButtonSize="4" HorizontalOptions="StartAndExpand" VerticalOptions="CenterAndExpand" Margin="3"></nanoXam:CircleButton>
                                        </StackLayout>
                                    </ViewCell.View>
                                </ViewCell>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
    
  • @DavidBritch said:
    The child of the DataTemplate element must be of or derive from type ViewCell.

    https://developer.xamarin.com/guides/xamarin-forms/user-interface/listview/customizing-cell-appearance/#Custom_Cells

    Thanks.
    But why don't the Xamarin team at microsoft put this message in the exception?
    You just get an error that literally tells you nothing and than have to crawl the internet to find out what's going wrong...

    I'd love to see a message saying "Invalid argument given for DataTemplate.ctor(). Given argument type StackLayout, Expected argument type ViewCell".

    This really can't be that hard and would prevent a lot of pain...

  • foamsterfoamster Member ✭✭

    Two years later, this is still an issue. Just had this problem today

Sign In or Register to comment.