1
votes

I have 2 listviews in my page. First list shows categories horizontally. Second list shows corresponding products below upon clicking category. Each list item of second list contains plus and minus buttons to add product to the cart. Label updates the count of each product upon clicking buttons.Works perfect for the first time. label is not updating in ui after coming from other category.Button clicks are working, While debugging, label value is updated correctly in viewmodel but not reflected in ui since second time.Need help..

xaml:

<ListView  x:Name="pdt_list" HasUnevenRows="True" SeparatorVisibility="None" ItemsSource="{Binding Productlist}" BackgroundColor="White" Margin="0,10,0,0" >
            <ListView.ItemTemplate>
                <DataTemplate >
                    <ViewCell >
                        <ViewCell.View>
                             <Frame HasShadow="False" Margin=" 0,10,0,10"  Padding="10,5,10,5" BackgroundColor="#e9e9e9" HeightRequest="80" HorizontalOptions="FillAndExpand" VerticalOptions="CenterAndExpand" >
                                  <Grid>
                                            <!--<StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="CenterAndExpand"  Margin="0,10,0,10"  >-->

                                  <StackLayout VerticalOptions="FillAndExpand" Margin="0" Padding="10,0,0,0" Orientation="Horizontal" Opacity="{Binding opacity}">

                                     <Image Source="{Binding image}"  Aspect="AspectFill"  WidthRequest="70" HeightRequest="180"  VerticalOptions="FillAndExpand" />

                                      <StackLayout HorizontalOptions="FillAndExpand" Orientation="Vertical"  >

                                        <Label Text="{Binding product_name}" Font="Bold" VerticalTextAlignment="Center" FontSize="Medium" TextColor="Black" FontFamily="opensans_light.ttf#opensans_light" Margin="10,20,0,0" />
                                            <StackLayout Orientation="Horizontal" Margin="10,0,0,0" HorizontalOptions="Start" VerticalOptions="Start"  >
                                                        <Label Text="{Binding rupee}"  TextColor="#FA2E27" HeightRequest="90" 
                                                            FontSize="Medium" HorizontalOptions="Start" VerticalTextAlignment="Start" VerticalOptions="FillAndExpand" />
                                                        <Label Text="{Binding selling_price}" Font="Bold"  HorizontalOptions="Start" Margin="0" TextColor="#FA2E27" VerticalTextAlignment="Start" FontSize="Medium" FontFamily="opensans_light.ttf#opensans_light" />

                                           </StackLayout>
                                      </StackLayout>
                                      <ImageButton Source="carts.png" BackgroundColor="Transparent" IsVisible="False" HorizontalOptions="EndAndExpand" WidthRequest="40" HeightRequest="40" VerticalOptions="CenterAndExpand"  Clicked="Add_cart_Clicked" Margin="0,20,0,0" Aspect="AspectFit" />


                                    <StackLayout Orientation="Horizontal" BackgroundColor="Transparent"   HorizontalOptions="EndAndExpand">
                                        <ImageButton Source="minus.png" HorizontalOptions="EndAndExpand"   BackgroundColor="Transparent"  WidthRequest="25" HeightRequest="25" Clicked="Minus_Tapped" />
  //this label is not updating          <Label Text="{Binding Num}" VerticalTextAlignment="Center" HorizontalOptions="EndAndExpand"   FontSize="Medium" TextColor="Black" FontFamily="opensans_light.ttf#opensans_light" Margin="0,0,0,0" /> 
                                        <ImageButton Source="plus2.png" HorizontalOptions="EndAndExpand"   BackgroundColor="Transparent"   WidthRequest="25" HeightRequest="25"  Clicked="Plus_Tapped"   />
                                    </StackLayout>
                                            </StackLayout>

                                            <StackLayout BackgroundColor="Black" HorizontalOptions="EndAndExpand" VerticalOptions="StartAndExpand" WidthRequest="100" HeightRequest="25" IsVisible="{Binding opaque}" Margin="0,0,0,0" >

                                                <Label Text="Not Available" FontFamily="opensans_light.ttf#opensans_light" TextColor="White" FontAttributes="Bold" HorizontalOptions="Center" VerticalTextAlignment="Center" />
                                            </StackLayout>
                                  </Grid>
                             </Frame>
                        </ViewCell.View>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

viewmodel:

After clicking plus button, this code is executed:

public ObservableCollection<Product_Value2> purchaselist = new ObservableCollection<Product_Value2>();

public void Plus_item(Product_Value2 product_Value2)
    {
        var list = new ObservableCollection<Product_Value2>(purchaselist);


        string id = product_Value2.id;
        ProductsRegister reg = new ProductsRegister();

        reg.Name_Product = product_Value2.product_name;
        App.Database.SaveProducts(reg);



        if ((purchaselist.ToList() != null) && (purchaselist.ToList().Any()))
        {
            //  bool alreadyExists = purchaselist.Any(x => x.id.Equals(id));
            if (purchaselist.Any(x => x.id.Equals(id)))
            {
                foreach (Product_Value2 pdt in list)
                {
                    if (pdt.id.Equals(id))
                    {
                        pdt.Num++; // here value is updating everytime,but first time only in ui
                        OnPropertyChanged("Num");
                        break;
                    }

                }
            }



        }



        DependencyService.Get<IToast>().ShortAlert("Added To Cart");

        Number++;
        OnPropertyChanged("Number");

    }

Model:

public class Product_Value2 : INotifyPropertyChanged
    {         
        public string id { get; set; }
        public string image { get; set; }
        public string imagepath { get; set; }
        public string product_name { get; set; }
        public string rupee { get; set; }
        public float selling_price { get; set; }
        public float price { get; set; }
        public string available_qty { get; set; }
        public string count { get; set; }        
        public bool minus_enb { get; set; }
        public bool plus_enb { get; set; }


        bool Visibility;
        public bool visibility
        {
            set
            {
                Visibility = value;
                OnPropertyChanged("visibility");
            }
            get
            {
                return Visibility;
            }
        }

        long num1 ;
        public long Num  //this is the label text
        {
            set
            {
                num1 = value;
                OnPropertyChanged("Num");
            }
            get
            {
                return num1;
            }
        }


        bool Visible;
        public bool visible
        {
            set
            {
                Visible = value;
                OnPropertyChanged("visible");
            }
            get
            {
                return Visible;
            }
        }




        void OnPropertyChanged(string IsVisible)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(IsVisible));
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }

Attaching the screenshot below shows initial:

first time

below shows secondtime:

second time

2
added. but not working.. - Vipin Krishna
added this in viewmodel class below pdt.Num++; OnPropertyChanged("Num"); still not.. - Vipin Krishna
You can try this Text="{Binding Num, Source={x:Reference Productlist}" may be need another binding because Lebel inside viewcell - R15
i put this: Text="{Binding Num, Source={x:Reference pdt_list}}" . now label text not showing first - Vipin Krishna

2 Answers

2
votes

I think the parameter you are passing to the method Plus_item(Product_Value2 product_Value2) is the item that you are trying to add or subtract. So you can simply update the Num in the incoming parameter itself. You are finding from the list and updating in the list based on the id. Below example will give you more clarity,

async void Plus_Tapped(object sender, System.EventArgs e)
    {
        Product_Value2 product_Value2 = ((Product_Value2)((ImageButton)sender).BindingContext);
        if (product_Value2 != null)
        {
            product_Value2.Num++;
        }
    }

    async void Minus_Tapped(object sender, System.EventArgs e)
    {
        Product_Value2 product_Value2 = ((Product_Value2)((ImageButton)sender).BindingContext);
        if (product_Value2 != null)
        {
            product_Value2.Num--;
        }
    }
0
votes

i just made some changes to the code.

                if (purchaselist.Any(x => x.id.Equals(id)))
                {
                    foreach (Product_Value2 pdt in list)
                    {
                        if (pdt.id.Equals(id))
                        {
                            pdt.Num++; // here value is updating everytime,but first time only in ui
                            product_Value2.Num  = pdt.Num; 


                        }

                    }
                }

Now ui is updating everytime.Working fine...