I am simply trying to catch an item in my observable collection and change one of its props:
public void Click(object obj)
{
Device.InvokeOnMainThreadAsync(() =>
{
if (obj != null)
{
var click = obj as WifiSelectionModel;
if (click == null) return;
click.IsActive = true;
}
OnPropertyChanged(nameof(WifiHotSpots));
});
My class inherits from : ObservableObject,
If I click, nothing changes in the UI (it should become red).
But if I leave the page and return; the items are now all red. So my logic must be correct, only the way I handle the UI in mvvm seems off.
Any help?
VM:
public class WifiSelectionModel
{
public string Name { get; set; } = "";
public bool IsActive { get; set; } = false;
public int Id { get; set; }
}
View:
<CollectionView SelectionMode="Single" VerticalOptions="FillAndExpand" ItemsSource="{Binding WifiHotSpots}">
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="Models:WifiSelectionModel">
<Grid Padding="10">
<Grid.RowDefinitions>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition Height="50"></RowDefinition>
</Grid.RowDefinitions>
<Grid.GestureRecognizers>
<TapGestureRecognizer Command="{Binding BindingContext.ClickCommand, Source={x:Reference WifiPageViewPage}}" CommandParameter="{Binding .}"/>
</Grid.GestureRecognizers>
<Label Grid.Row="0" Text="{Binding Name}" FontAttributes="Bold" />
<StackLayout HeightRequest="50" IsVisible="{Binding IsActive}" BackgroundColor="Red" Grid.Row="1">
<Label Text="hi mom"></Label>
</StackLayout>
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>