Home > Software design >  Specified cast is not valid with observable collection
Specified cast is not valid with observable collection

Time:11-15

I'm trying to add items to an ObservableCollection that is bound to a ListView.

Definition:

public ObservableCollection<Category> SearchCategoriesResults { get; set; } = new();

XAML

<ListView ItemsSource="{Binding SearchCategoriesResults}"
          BackgroundColor="{StaticResource Primary}"
          SeparatorVisibility="None"
          WidthRequest="250"
          RowHeight="30"
          HeightRequest="150">
    <ListView.ItemTemplate>
        <DataTemplate x:DataType="model:Category">
            <Label Text="{Binding Name}" />
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

And the try/catch block where I get the exception 'Specified cast is not valid'

public void SearchCategories()
{
    SearchCategoriesResults.Clear();
    try
    {
        //Trying arbitrary data
        SearchCategoriesResults.Add(new Category { Id = 15, Name = "Name" });
    }
    catch(Exception ex)
    {
        //Specified cast is not valid
        Debug.WriteLine(ex.Message);
    }
}

If I copy a CollectionView from a different area of my app, it works fine:

<CollectionView ItemsSource="{Binding SearchCategoriesResults}"
            SelectionMode="Single"
            SelectedItem="{Binding SearchCategoriesSelectedResult, Mode=TwoWay}"
            HeightRequest="120"
            WidthRequest="250">
    <CollectionView.EmptyView>
        <StackLayout Padding="50">
            <Label TextColor="White"
                    FontAttributes="Bold"
                    HorizontalOptions="CenterAndExpand"
                    Text="NO DATA"
                    VerticalOptions="CenterAndExpand" />
        </StackLayout>
    </CollectionView.EmptyView>
    <CollectionView.ItemsLayout>
        <LinearItemsLayout Orientation="Vertical" />
    </CollectionView.ItemsLayout>

    <CollectionView.ItemTemplate>
        <DataTemplate x:DataType="model:Category">
            <Grid HeightRequest="40"
                Margin="5,3,5,3"
                Padding="5,0,5,0"
                ColumnDefinitions="*"
                RowDefinitions="*,*"
                BackgroundColor="{StaticResource Secondary}">

                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup Name="CommonStates">
                        <VisualState Name="Selected">
                            <VisualState.Setters>
                                <Setter Property="BackgroundColor" Value="{StaticResource Fourth}" />
                            </VisualState.Setters>
                        </VisualState>
                        <VisualState Name="Normal">
                            <VisualState.Setters>
                                <Setter Property="BackgroundColor" Value="{StaticResource Secondary}" />
                            </VisualState.Setters>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>
                <Label Grid.Row="0" TextColor="White" Text="{Binding Name}" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" />
            </Grid>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

I'm not sure why the CollectionView works while the ListView doesn't. Anyone have any ideas?

CodePudding user response:

ListView needs a ViewCell. (CollectionView does not.) Try:

<ListView.ItemTemplate>
    <DataTemplate x:DataType="model:Category">
        <ViewCell>
            <Label Text="{Binding Name}" />
        </ViewCell>
    </DataTemplate>
</ListView.ItemTemplate>

CodePudding user response:

You can try to replace the following code :

 public ObservableCollection<Category> SearchCategoriesResults { get; set; } = new();

with:

 public ObservableCollection<Category> SearchCategoriesResults { get; set; } = ObservableCollection<Category>();
  • Related