Forum Xamarin.Forms

[Beginner]How can i get buttons to call the increase/decrease methods for 2 objects in a list?

THenny12THenny12 Member ✭✭
edited April 2019 in Xamarin.Forms

I have a listview of objects each with two buttons that i want to increment/decrement a variable on a object
<ListView.ItemTemplate> <DataTemplate> <ViewCell> <StackLayout Orientation="Horizontal" HorizontalOptions="Fill"> <Label Text="{Binding Name}" FontSize="24"/> <Button x:Name="btn_minus" Text="-"Clicked="btn_minus_Clicked"/> <Label Text="{Binding Quantity, Mode=TwoWay}" FontSize="24" /> <Button x:Name="btn_plus"Text="+"FontSize="24"Clicked="btn_plus_Clicked"/> </StackLayout> </ViewCell>
Each object has a quantity and two functions to increment/decrement
public class GatorStick { public int Quantity { get; set; } public GatorStick() { Quantity = 0; } public void IncreaseQuantity() { ++Quantity; } public void DecreaseQuantity() { if (Quantity == 0) { } else --Quantity; } }

How can i get those buttons to call the increase/decrease methods for each object in the list independent of each other?
Sorry for the bad formatting i tried everything to get it correct.

Best Answer


  • THenny12THenny12 Member ✭✭

    Hey @chetanrawat, thank you for responding! I added your code to the code behind and the Quantity variable doesn't update when I run the application and press the button. Do you know what the problem is?

  • chetanrawatchetanrawat USMember ✭✭✭

    Have you created Observable List with In INotifyPropertyChanged Event?
    private ObservableCollection ListName= new ObservableCollection();

  • Sumit_SharmaSumit_Sharma USMember ✭✭✭
    edited April 2019


    how would you get x:name of a lable control which is inside the listview, if you have read the question correctly?

    EDIT: Yelinzh has removed his answer.

  • THenny12THenny12 Member ✭✭
    edited April 2019

    @chetanrawat yes i did create an ObservableCollection with my objects in it

  • chetanrawatchetanrawat USMember ✭✭✭

    @THenny12 are you using MVVM?

  • THenny12THenny12 Member ✭✭

    @chetanrawat No, i had that in the code behind file for that page. How would i go about implementing mvvm?

  • JarvanJarvan Member, Xamarin Team Xamurai

    You can try to use stepper and create a custom steppter.

        <ListView x:Name="list_view">
                        <StackLayout Orientation="Horizontal" HorizontalOptions="Fill">
                            <Label Text="label" 
                            <Label BindingContext="{x:Reference cst_stepper}" 
                                   Text="{Binding Value}"


    public class CustomStepper : StackLayout
        Button PlusBtn;
        Button MinusBtn;
        Label label;
        public static readonly BindableProperty TextProperty =
             propertyName: "Text",
              returnType: typeof(int),
              declaringType: typeof(CustomStepper),
              defaultValue: 1,
              defaultBindingMode: BindingMode.TwoWay);
        public int Text
            get { return (int)GetValue(TextProperty); }
            set { SetValue(TextProperty, value); }
        public CustomStepper()
            PlusBtn = new Button { Text = "+", WidthRequest = 40, FontAttributes = FontAttributes.Bold, FontSize = 18 };
                MinusBtn = new Button { Text = "-", WidthRequest = 40, FontAttributes = FontAttributes.Bold, FontSize = 18 };
            switch (Device.RuntimePlatform)
                case Device.UWP:
                case Device.Android:
                        PlusBtn.BackgroundColor = Color.Transparent;
                        MinusBtn.BackgroundColor = Color.Transparent;
                case Device.iOS:
                        PlusBtn.BackgroundColor = Color.DarkGray;
                        MinusBtn.BackgroundColor = Color.DarkGray;
            Orientation = StackOrientation.Horizontal;
            PlusBtn.Clicked += PlusBtn_Clicked;
            MinusBtn.Clicked += MinusBtn_Clicked;
            label = new Label
                TextColor = Color.Gray,
                FontSize = 18,
                VerticalOptions = LayoutOptions.CenterAndExpand,
                BackgroundColor = Color.FromHex("#3FFF")
            label.SetBinding(Label.TextProperty, new Binding(nameof(Text), BindingMode.TwoWay, source: this));
        private void MinusBtn_Clicked(object sender, EventArgs e)
            if (Text >= 1)
        private void PlusBtn_Clicked(object sender, EventArgs e)
Sign In or Register to comment.