iNCEPTiON_
iNCEPTiON_

Reputation: 621

Xamarin.Forms Binding Specified cast is not valid

I have a weird exception where the Compiler tells me that the Specified cast is not valid even though what im doing is very Simple.

I have a ListView binded to a ObservableCollection. And inside my Listview is a ViewCell with a Grid. Xamarin.Forms Version 2.3.2.127

<ListView ItemsSource="{Binding GiftCollection}">
      <ListView.ItemTemplate>
        <DataTemplate>
          <ViewCell>
            <ViewCell.View>
              <Grid>
          <Grid.RowDefinitions>
            <RowDefinition Height="20"/>
            <RowDefinition Height="20"/>
          </Grid.RowDefinitions>
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="40"/>
          </Grid.ColumnDefinitions>

          <Label Grid.Row="0" Grid.Column="0" Text="{Binding GiftName}"/>
          <Label Grid.Row="1" Grid.Column="0" Text="{Binding GiftDescription}"/>
          <Image Grid.Row="0" Grid.RowSpan="2" Grid.Column="1" Source="{Binding GiftImage}"/>
        </Grid>
        </ViewCell.View>
      </ViewCell>
        </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>

Model:

 public class GiftModel {

        public string GiftName { get; set; }
        public string GiftDescription { get; set; }
        public ImageSource GiftImage { get; set; }
    }

ViewModel:

public class NextRoundViewModel : BaseViewModel {

        public NextRoundViewModel(ApplicationModel applicationModel) {
            ApplicationModel = applicationModel;
            Initialize();
        }

        public ApplicationModel ApplicationModel { get; set; }
        public ObservableCollection<GiftModel> GiftCollection { get; set; }
        public string CurrentRound => "Runde 2";

        private void Initialize() {
            GiftCollection = new ObservableCollection<GiftModel> {
                new GiftModel {
                    GiftName = "100 Punkte",
                    GiftDescription = "Test",
                    GiftImage = ImageSource.FromFile("Star.png"),
                },
                new GiftModel {
                    GiftName = "200 Punkte",
                    GiftDescription = "Test",
                    GiftImage = ImageSource.FromFile("Star.png"),
                },
                new GiftModel {
                    GiftName = "300 Punkte",
                    GiftDescription = "Test",
                    GiftImage = ImageSource.FromFile("Star.png"),
                },
            };
        }
    }

So ive tried everything but if i use for example a TextCell the Exception is gone. System.InvalidCastException: Specified cast is not valid. It is just weird because i dont know where to look for the Bug.

Upvotes: 21

Views: 33834

Answers (4)

toumir
toumir

Reputation: 641

The following code gives the same exception, may help someone: using ListView or CollectionView but in the ItemTemplate I use BindableLayout.ItemTemplate instead CollectionView.ItemTemplate

<CollectionView Grid.Row="0" ItemsSource="{Binding SummaryInfos}">
        <CollectionView.ItemsLayout>
            <LinearItemsLayout Orientation="Horizontal" />
        </CollectionView.ItemsLayout>

        <!-- this code gives the same exception -->
        <!-- must use CollectionView instead BindableLayout -->
        <BindableLayout.ItemTemplate>
            <DataTemplate>
                <StackLayout>
                    <Label Text="{Binding SomeText}"/>
                </StackLayout>
            </DataTemplate>
        </BindableLayout.ItemTemplate>
</CollectionView>

Upvotes: 0

Guest
Guest

Reputation: 61

Remove the <ViewCell> from the DataTemplate. This should resolve the error. <Grid> will work in <CollectionView>

 <ListView.ItemTemplate>
     <DataTemplate>
         <Grid>
         :    
         :    
         </Grid>
     </DataTemplate>
 </ListView.ItemTemplate>

Upvotes: 6

Crennotech
Crennotech

Reputation: 519

You can place inside with .

Something like this

<ListView.ItemTemplate>
<DataTemplate>
     <ViewCell>
          <StackLayout>
          </StackLayout>
     </ViewCell>
</DataTemplate>

Upvotes: 17

Muhammad Khan
Muhammad Khan

Reputation: 1031

I had this problem too, the issue was with the xaml. I had a <StackLayout> inside of my <DataTemplate>, you can remove your <Grid> and that should solve the problem.

Did you know that you could replace the <Grid> with an <ImageCell>:

<ListView.ItemTemplate>
    <DataTemplate>
        <ImageCell
            Text="{Binding GiftName}"
            Detail="{Binding GiftDescription}"
            ImageSource="{Binding GiftImage}">
        </ImageCell>
    </DataTemplate>
</ListView.ItemTemplate>

Upvotes: 32

Related Questions