I have a grouped listview in my project. When I try to add the ItemSelected
event for the listview I am getting the following error:
System.InvalidCastException: 'Specified cast is not valid.'
My Code
XAML:
<ListView
ItemsSource="{Binding AllItems,Mode=TwoWay}">
<ListView.GroupHeaderTemplate>
<DataTemplate>
<ViewCell>
//header label
<Label/>
</ViewCell>
</DataTemplate>
</ListView.GroupHeaderTemplate>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40*"/>
<ColumnDefinition Width="60*"/>
</Grid.ColumnDefinitions>
<StackLayout
Grid.Column="0"
BackgroundColor="#f9f9f9"
Orientation="Vertical">
//Items
</StackLayout>
<Label Grid.Column="1"/>
</Grid>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.Footer>
<Label/>
</ListView.Footer>
</ListView>
Viewmodel
public ObservableCollection<EventsHB> AllItems
{
get
{
return _allItems;
}
set
{
_allItems = value;
OnPropertyChanged("AllItems");
}
}
XAML.CS
MyEventsListview.ItemSelected += (object sender, SelectedItemChangedEventArgs e) =>
{
var selectedItem = (EventsHB) e.SelectedItem;
if (selectedItem != null)
{
//loading the next page
}
MyEventsListview.SelectedItem = null;
};
SelectedItem
is? – Miamy