Jochem
Jochem
CC#
Created by Jochem on 6/30/2023 in #help
✅ MAUI UI won't update after calling OnPropertyChanged from Command
thank you! 👍
4 replies
CC#
Created by Jochem on 6/30/2023 in #help
✅ MAUI UI won't update after calling OnPropertyChanged from Command
public class Item
{
public Color Color { get; set; } = Colors.Red;
}

public class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

public List<Item> List { get; set; } = new();

public ICommand ButtonPressedCommand { get; set; }

public ViewModel()
{
List.Add(new Item());
List.Add(new Item());
List.Add(new Item());

ButtonPressedCommand = new Command<Item>(ButtonPressed);
}

public void ButtonPressed(Item item)
{
item.Color = Colors.Green;
OnPropertyChanged(nameof(List));
}

public void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class Item
{
public Color Color { get; set; } = Colors.Red;
}

public class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

public List<Item> List { get; set; } = new();

public ICommand ButtonPressedCommand { get; set; }

public ViewModel()
{
List.Add(new Item());
List.Add(new Item());
List.Add(new Item());

ButtonPressedCommand = new Command<Item>(ButtonPressed);
}

public void ButtonPressed(Item item)
{
item.Color = Colors.Green;
OnPropertyChanged(nameof(List));
}

public void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Test"
x:Class="Test.MainPage"
x:DataType="local:ViewModel">

<ScrollView>
<VerticalStackLayout
Spacing="25"
Padding="30,0"
VerticalOptions="Center">

<CollectionView
ItemsSource="{Binding List}">

<CollectionView.ItemTemplate>
<DataTemplate>
<Button
x:DataType="local:Item"
Text="Press me"
BackgroundColor="{Binding Color}"
CommandParameter="{Binding .}"
Command="{Binding Source={RelativeSource AncestorType={x:Type local:ViewModel}}, Path=ButtonPressedCommand}"/>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>

</VerticalStackLayout>
</ScrollView>

</ContentPage>
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Test"
x:Class="Test.MainPage"
x:DataType="local:ViewModel">

<ScrollView>
<VerticalStackLayout
Spacing="25"
Padding="30,0"
VerticalOptions="Center">

<CollectionView
ItemsSource="{Binding List}">

<CollectionView.ItemTemplate>
<DataTemplate>
<Button
x:DataType="local:Item"
Text="Press me"
BackgroundColor="{Binding Color}"
CommandParameter="{Binding .}"
Command="{Binding Source={RelativeSource AncestorType={x:Type local:ViewModel}}, Path=ButtonPressedCommand}"/>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>

</VerticalStackLayout>
</ScrollView>

</ContentPage>
4 replies
CC#
Created by Jochem on 6/17/2023 in #help
❔ HTTP request object creation / ignoring fields when deserializing JSON
Yes, I read about custom converters but didn't know it could do that automatically? does it work when the enum name is not case-sensitive equal to the json value?
31 replies
CC#
Created by Jochem on 6/17/2023 in #help
❔ HTTP request object creation / ignoring fields when deserializing JSON
Okay thank you both for the responses!
31 replies