Home > other >  Update the object of ObservableCollection is not reflecting the in the UI
Update the object of ObservableCollection is not reflecting the in the UI

Time:11-12

I am binding ObservableCollection with CollectionView.

<CollectionView ItemsSource="{Binding LeftExercises}">
                                    <CollectionView.ItemTemplate>
                                        <DataTemplate x:DataType="models:ExerciseModel">
                                            <Grid>
                                                <Grid.RowDefinitions>
                                                    <RowDefinition Height="50"/>
                                                </Grid.RowDefinitions>
                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="3*" />
                                                    <ColumnDefinition Width="4*" />
                                                    <ColumnDefinition Width="4*" />
                                                </Grid.ColumnDefinitions>
                                                <Label Grid.Column="0" Grid.Row="0" Text="{Binding SetNumber}" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" TextColor="Black" FontSize="Medium" />
                                                <Label Grid.Column="1" Grid.Row="0" Text="{Binding Weight}" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" TextColor="Black" FontSize="Medium" />
                                                <Label Grid.Column="2" Grid.Row="0" Text="{Binding Reps}" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" TextColor="Black" FontSize="Medium" />
                                            </Grid>
                                        </DataTemplate>
                                    </CollectionView.ItemTemplate>
                                </CollectionView>

private ObservableCollection<ExerciseModel> _leftExercises;
        public ObservableCollection<ExerciseModel> LeftExercises
        {
            get => _leftExercises;
            set
            {
                if (_leftExercises != value)
                {
                    _leftExercises = value;
                    OnPropertyChanged(nameof(LeftExercises));
                }
            }
        }

When I add a new object to the Collection, it will reflect in my UI but whenever I try to update the value of any object, it will not reflect.

This is my model

public class ExerciseModel
    {
        public int SetNumber { get; set; }
        public decimal Weight { get; set; }  
        public int Reps { get; set; } 
        public ExerciseType ExerciseType { get; set; }
        public Side Side { get; set; }
    }

I am incrementing the Reps (update Reps property) from the below command.

private Command _dummyLeftIncreaseRepsCommand;
        public Command dummyLeftIncreaseRepsCommand
        {
            get
            {
                return _dummyLeftIncreaseRepsCommand ??= new Command(() =>
                {
                    ExerciseModel lastObj = LeftExercises.Last(x => x.Side == SharedVM.ActiveSide);
                    lastObj.Reps  = 1;
                });
            }
        } 

CodePudding user response:

Your ExerciseModel class needs to implement INotifyPropertyChanged.

To do so, simply add the interface name to your class code, like this:

class MyClass : INotifyPropertyChanged

Alternatively, you can set the ItemsSource for the object again. Doing so will update the visual as well. You can do this at runtime like this:

XAML file:

<CollectionView x:Name="MyView">[...]

Code in background:

MyView.ItemsSource = MyCollectionVar;

CodePudding user response:

I advise you to use CommunityToolkit.MVVM and CommunityToolkit.MAUI, when you try to bind commands, observable properties and collections to your XAML.

The benefit of this will be that you wont have to write walls of code, and spend hours to figure out why it is not working.

It saves you time, that you can put in something useful, instead of dealing with boilerplate code.

  • Related