0

i m trying to do a project with mvvm xamarin.

I have a listview ( bounded a observablecollection), and inside the listview, i have a button. with clicking this button, i make some updates in Observable collection. But the updates, doesnt effect untill i remove and again insert the Observable collection.

here is the code


 <ListView ItemsSource="{Binding SalesList,Mode=TwoWay}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell IsEnabled="True"    >

                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>


                            <Label FontSize="Large" Text="{Binding StockTotal}"/>

                            <Button Text="+"     Grid.Column="1"  CommandParameter="{Binding .}"   Command="{Binding Path=BindingContext.ADDOPERATION,    Source={x:Reference Mypage}}"  />

                            <Entry Text="{Binding StockAmount}"  Grid.Column="2" />

                        </Grid> 
                    </ViewCell>
                </DataTemplate>  
            </ListView.ItemTemplate> 
        </ListView>

    <Label Text="{Binding SUMMARY}"/>




 public ICommand ADDOPERATION { get; set; }
        public HomeVM()
        {

            SalesList.Add(new SalesModel { StockAmount = 1 });
            SalesList.Add(new SalesModel { StockAmount = 2 });
            SalesList.Add(new SalesModel { StockAmount = 3 });

            ADDOPERATION = new Command(MYADDOPERATION);
        }
        private void MYADDOPERATION(object obj)
        {
            if (obj == null) return;

            var cominginfo = (SalesModel)obj;


            var index= SalesList.IndexOf(cominginfo);


            cominginfo.StockAmount++;

            cominginfo.StockTotal = cominginfo.StockAmount * 55;

 }


 private double _SUMMARY; 
        public double SUMMARY
        {
            get { return SalesList.Sum(c => c.StockTotal); }
            set
            {
                _SUMMARY = SalesList.Sum(c=>c.StockTotal);
                INotifyPropertyChanged();
            }
        }

I don't know why this problem is occured. the ADDOPERATION command works successfuly ( i checked it in debuge mode), but it doesnt update the listview. List view updates when i add this code to below. But i think i m missing something, because, when ever i make changes inside the ObservableCollection, it should automaticly effect the UI. I m able to do it only after i remove and again insert the same data.

            SalesList.RemoveAt(index);

            SalesList.Insert(index, cominginfo); 

the BaseViewModel class for INotifyPropertyChanged event

  public class BaseViewModel : INotifyPropertyChanged
    { 
        public event PropertyChangedEventHandler PropertyChanged;
        protected void INotifyPropertyChanged([CallerMemberName] string propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

my model class

 public class SalesModel:BaseViewModel
    {

        public double StockTotal { get; set; } 
        public string StockName { get; set; }
        public double StockAmount { get; set; }
    }

Thanks in advance

11
  • 1
    SalesModel needs to implement INotifyPropertyChanged. ObservableCollection only handles additions and changes to the collection, not changes to properties of items that are in the collection. Commented Mar 19, 2020 at 14:30
  • 1
    See this SO Post about ObservableCollection not noticing when Item in it changes (even with INotifyPropertyChanged), in resume: Observable collection only notifies when an item is added, removed, moved, but not when an item changes, so you need to implement the property change on your Collection Properties Commented Mar 19, 2020 at 14:31
  • You can also set ItemSource to null and then back to new Values, but its not ideal. Commented Mar 19, 2020 at 15:41
  • 1
    please edit your question to include any relevant code instead of stuffing it into a comment Commented Mar 19, 2020 at 20:30
  • 1
    does SalesModel inherit BaseViewModel? Commented Mar 19, 2020 at 20:37

1 Answer 1

1

you need to call PropertyChanged on the setters in order to notify the UI that the value has changed

private double stockTotal;
public double StockTotal 
{ 
  get
  {
    return stockTotal;
  }
  set 
  {
     stockTotal = value;
     PropertyChanged("StockTotal");
  }
}
Sign up to request clarification or add additional context in comments.

5 Comments

It worked. thank you sir. I have just got the idea. best regards.
By the way, it only works for only one property. If i add PropertyChanged("StockAmount"); it doesnt work. app crashes.
Did you add a backing field for that property? If it crashes you’re doing something wrong.
it s okey now thank u sir. On the other hand, i can not get the sum of a specific column. SUMMARY property doesnt update despite the ObservableCollection updates. I updated the code
SUMMARY is not a true property, it is computed. So you should raise PropertyChanged("Summary") whenever StockTotal is set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.