Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Not able to get the text property of custom stepper used in the xaml file to its cs file in xamarin

$
0
0

Here group binding is done and the products are listed under the corresponding dealers. I want to multiply the price and quantity of each products under one dealer and store it in a List_model-OrderModel when the View Order button is clicked. I am not able to access the quantity value in the custom stepper text. I tried var qty = stepper.Text. but the name stepper is not coming. CustomStepper and Stepper is coming the dropdown.

This is my custom stepper:

/*Created this class to make a custon stepper to add quantity */
public class CustomStepper : StackLayout
{
Button PlusBtn;
Button MinusBtn;
Label QtyLbl;

public static readonly BindableProperty TextProperty =
  BindableProperty.Create(
     propertyName: "Text",
      returnType: typeof(int),
      declaringType: typeof(CustomStepper),
      defaultValue: 0,
      defaultBindingMode: BindingMode.TwoWay);

public int Text
{
    get { return (int)GetValue(TextProperty); }
    set { SetValue(TextProperty, value); }
}

public CustomStepper()
{
    PlusBtn = new Button { Text = "+", WidthRequest = 23, HeightRequest = 25, FontSize = 11, BackgroundColor = Color.LightGray, Padding = 0};
    MinusBtn = new Button { Text = "-", WidthRequest = 23, HeightRequest = 25, FontSize = 11, BackgroundColor = Color.LightGray, Padding = 0};

    Orientation = StackOrientation.Horizontal;
    PlusBtn.Clicked += PlusBtn_Clicked;
    MinusBtn.Clicked += MinusBtn_Clicked;

    QtyLbl = new Label { FontSize = 11, VerticalTextAlignment = TextAlignment.Center, HorizontalTextAlignment = TextAlignment.Center, WidthRequest = 20   };
    QtyLbl.SetBinding(Entry.TextProperty, new Binding(nameof(Text), BindingMode.TwoWay, source:this));

    Children.Add(MinusBtn);
    Children.Add(QtyLbl);            
    Children.Add(PlusBtn);
}

private void MinusBtn_Clicked(object sender, EventArgs e)
{
    if (Text > 0)
        Text--;
}

private void PlusBtn_Clicked(object sender, EventArgs e)
{
    if (Text < 10)
        Text++;
}

}
This is my view-model code:

public class CustomerDetailViewModel : BaseViewModel
{
public int ItemQuantity { get; set; }
}
this is how i have added my custom stepper my xaml file:


I have this in my button click in the corresponding cs file:

private async void viewOrderBtn_Clicked(object sender, EventArgs e)
{
var item = (CustomStepper)sender; //no idea if this is correct
var qty = //not able to get the custom stepper text property.

    var itemobj = item?.BindingContext as AllProductListview;
    List<OrderModel> Orderlist = new List<OrderModel>();
    int ID = Convert.ToInt32(itemobj.PK);
    int vendorfk = Convert.ToInt32(itemobj.VendorFK);
    float price = itemobj.SalesPrice;
    float total = price * quantity;
    Orderlist.Add(new OrderModel
    {
        PK = Convert.ToInt32(itemobj.PK),
        VendorFK = Convert.ToInt32(itemobj.VendorFK),
        NameE = itemobj.NameE,
        NameC = itemobj.NameC,
        SalesPrice = itemobj.SalesPrice,
        DChargeMode = Convert.ToInt32(itemobj.DChargeMode),
        VatForm = itemobj.VatForm,
        DeliveryCharge = itemobj.DeliveryCharge,
        Quantity = quantity,
        TotalAmount = total,
        Delete = "delete.png"
    });
    if (OrderData.Count == 0)
    {
        OrderData.Add(new OrderModel
        {
            PK = Orderlist[0].PK,
            VendorFK = Orderlist[0].VendorFK,
            NameE = Orderlist[0].NameE,
            NameC = Orderlist[0].NameC,
            SalesPrice = Orderlist[0].SalesPrice,
            DChargeMode = Orderlist[0].DChargeMode,
            VatForm = Orderlist[0].VatForm,
            DeliveryCharge = Orderlist[0].DeliveryCharge,
            Quantity = Orderlist[0].Quantity,
            TotalAmount = Orderlist[0].TotalAmount,
            Delete = Orderlist[0].Delete
        });
        Isexecute = true;
    }
    else
    {
        if (vendorfk == OrderData[0].VendorFK)
        {
            for (int i = 0; i < OrderData.Count; i++)
            {
                /*@ambi : while debugging, when i change the qty of the 1st product and added the second product:
                 * [ID value is 47 and the OrderData[i].PK value is 46]*/
                if (ID == OrderData[i].PK)
                {
                    OrderData[i].Quantity = Orderlist[0].Quantity;
                    OrderData[i].TotalAmount = Orderlist[0].TotalAmount;
                    IsPresent = true;
                    Isexecute = true;
                }
            }
            if (IsPresent == false)
            {
                int count = OrderData.Count;
                for (int j = count - 1; j < count; j++)
                {
                    OrderData.Add(new OrderModel
                    {
                        PK = Orderlist[0].PK,
                        VendorFK = Orderlist[0].VendorFK,
                        NameE = Orderlist[0].NameE,
                        NameC = Orderlist[0].NameC,
                        SalesPrice = Orderlist[0].SalesPrice,
                        DChargeMode = Orderlist[0].DChargeMode,
                        VatForm = Orderlist[0].VatForm,
                        DeliveryCharge = Orderlist[0].DeliveryCharge,
                        Quantity = Orderlist[0].Quantity,
                        TotalAmount = Orderlist[0].TotalAmount,
                        Delete = Orderlist[0].Delete
                    });
                }
                Isexecute = true;
            }
        }
        else
        {
            await DisplayAlert(LangResource.Alert, "You must order from the same dealer only.", LangResource.ok);
            Isexecute = false;
        }
    }
    // Pickerindex = -1;
    if (Isexecute)
    {
        UploadPopUpPage orderPage = new UploadPopUpPage(OrderData);
        orderPage.EventPass += OrderPage_EventPass;
        await PopupNavigation.PushAsync(orderPage);
    }

}

Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>