2
votes

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 in my cs file. 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 Model class code:

public partial class AllProductListview
{
    public decimal PK { get; set; }
    public decimal VendorFK { get; set; }
    public string Vendorname { get; set; }
    public string distance { get; set; }
    public string NameE { get; set; }
    public string NameC { get; set; }
    public float SalesPrice { get; set; }
    public int Quantity { get; set; }  
    public decimal DChargeMode { get; set; }
    public string VatForm { get; set; }
    public float TotalAmount { get; set; }
    public string DeliveryCharge { get; set; }

  }

public class OrderModel
{
    public int PK { get; set; }
    public int VendorFK { get; set; }

    public string NameE { get; set; }
    public string NameC { get; set; }
    public float SalesPrice { get; set; }
    public int DChargeMode { get; set; }
    public string VatForm { get; set; }
    public string DeliveryCharge { get; set; }
    public int Quantity { get; set; }
    public float TotalAmount { get; set; }
    public string Delete { get; set; }
}

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

private async void viewOrderBtn_Clicked(object sender, EventArgs e)
    {
        var item = (Button)sender;  

        /*item.BindingContext contains all the values [whn i did step by step debugging] as in AllProductListview..now I have to put it in a loop and calculate the total by multiplying the price and quantity and store it in OrderModel by adding the record into the Orderlist*/
        var productList = item.BindingContext as AllProductListview; 
        List<OrderModel> Orderlist = new List<OrderModel>();
    }

My xaml file :

<Grid Grid.Row="3">
            <Frame HasShadow="True"
                   BackgroundColor="White"
                   Margin="1" 
                   Padding="5"> <!--@ambi : changed margin from 2 to 1 padding-10-->
                <ListView 
                        x:Name="Prodlistview"       
                        IsGroupingEnabled="True"
                        ItemsSource="{Binding GroupedData}"  
                        GroupDisplayBinding="{Binding Key}"
                        ItemSelected="Prodlistview_ItemSelected"
                        SeparatorVisibility="Default">

                    <ListView.GroupHeaderTemplate>
                        <DataTemplate>
                            <ViewCell Height="5"><!--@ambi : height-15-->
                                <StackLayout VerticalOptions="CenterAndExpand"  
                                             Padding="0,0"    
                                             Orientation="Horizontal"

                                            BackgroundColor="#e3e2de">
                                    <!--padding - 10  FontAttributes="Bold"-->
                                    <Label Text="{Binding Key}"                                                
                                           TextColor="Black" 
                                           VerticalOptions="CenterAndExpand" 
                                           HorizontalOptions="CenterAndExpand"
                                           FontSize="12" 
                                           Style="{StaticResource listlabel}"/>

                                    <Button x:Name="viewOrderBtn"
                                            Text= "{lang:Translate DriverViewOrder}"
                                            HorizontalOptions="EndAndExpand"
                                            FontAttributes="None"
                                            CommandParameter="{Binding Key}"
                                            Clicked="viewOrderBtn_Clicked"  
                                            Margin="2,5"
                                            Padding="1"
                                            FontSize="12"/> <!--@ambi : added button in the group view-->
                                </StackLayout>
                            </ViewCell>                                
                        </DataTemplate>

                    </ListView.GroupHeaderTemplate>

                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <local:ListViewSelectionColorRender SelectedBackgroundColor="Transparent">
                                <ViewCell.View>
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="*"/>                                                
                                            <ColumnDefinition Width="*"/>
                                            <ColumnDefinition Width="*"/>    <!--qty..width 0.55-->
                                        </Grid.ColumnDefinitions>
                                        <Label Grid.Column="0" Text="{Binding NameE}" 
                                               Style="{StaticResource listlabel}" MaxLines="1" 
                                               HorizontalTextAlignment="Start"
                                               LineBreakMode="TailTruncation"/>
                                        <!--@ambi : HorizontalTextAlignment="Start"-->
                                        <Label Grid.Column="1" Text="{Binding distance}" Style="{StaticResource listlabel}" 
                                               TextColor="DarkGoldenrod"/>

                                        <Label Grid.Column="2" Text="{Binding SalesPrice}" 
                                               Padding="0,0,15,0"
                                               HorizontalTextAlignment="End"
                                               Style="{StaticResource listlabel}"/>

                                        <StackLayout Grid.Column="3" VerticalOptions="Center" x:Name="Container">
                                            <!--<Frame BorderColor="Gray" HeightRequest="30" WidthRequest="40" 
                                                   CornerRadius="7" Padding="5,0" Margin="0,0" HasShadow="False">
                                                <local:BorderlessPicker x:Name="picker"
                                                                        Title="{lang:Translate Select}" 
                                                                        Style="{StaticResource picker}" 
                                                                        SelectedIndex="{Binding Pickerindex}"
                                                                        ItemsSource="{Binding QtyListModel}" 
                                                                        SelectedIndexChanged="QtyPicker_SelectedIndexChanged"                                                                                                                                              
                                                                        ItemDisplayBinding="{Binding Quantity}" 
                                                                        TextColor="Black"/>                                                    
                                            </Frame>-->                                                
                                            <!--@ambi : Custom stepper added to check a different method-->
                                                <local:CustomStepper x:Name="stepper"                                                                           
                                                                     Text="{Binding Quantity, Mode=TwoWay}"                                                                          
                                                                     Padding="3,0" />                                                
                                        </StackLayout>  
                                    </Grid>
                                </ViewCell.View>
                            </local:ListViewSelectionColorRender>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </Frame>
        </Grid>

Image : My screen

Update : I have attached the pic of my debug window. Please walk me from here. I could bring the data from the xaml. I'm new to xamarin forms. No idea how to go from here.

private async void viewOrderBtn_Clicked(object sender, EventArgs e)
    {            
        /*item.BindingContext contains all the values [whn i did step by step *debugging] as in AllProductListview..now I have to put it in a loop and *calculate the total by multiplying the price and quantity and store it in *OrderModel by adding the record into the Orderlist*/
        var item = (Button)sender;
        var itemobj = item?.BindingContext as AllProductListview;
        List<OrderModel> Orderlist = new List<OrderModel>();
        int ID = Convert.ToInt32(itemobj.PK); //showing no value assigned
        int vendorfk = Convert.ToInt32(itemobj.VendorFK); //showing no value assigned
        float price = itemobj.SalesPrice; //showing no value assigned
        int quantity = itemobj.Quantity; //showing no value assigned
        float total = price * quantity;
    }

Debug window

2
Which view does viewOrder belong to? The sender is not CustomStepper. Is that a listView?Jack Hua
" The sender is not CustomStepper" : I have corrected that in my code.Ambalika P
Can you try use viewOrderBtn.Parent.BindingContext to get the model of corresponding ViewCell?Jack Hua
@Jack Hua - MSFT : please check the attached imageAmbalika P
@Jack Hua - MSFT : Hey! i got the list of values there. now i want to get the Quantity from that listAmbalika P

2 Answers

1
votes

@chetan Thanks for the time and patience you took to help me out.

This is the final code.

 private async void viewOrderBtn_Clicked(object sender, EventArgs e)
    {
        var item = (Button)sender;
        List<OrderModel> Orderlist = new List<OrderModel>();
        bool Issame = true;
        OrderData.Clear();            
        int vendorfk;
        foreach (var items in GroupedData)
        {                
            foreach (var orderdet in items)
            {
                var productname = orderdet.NameE; 
                float price = orderdet.SalesPrice;
                int quantity = orderdet.Quantity;
                float total = price * quantity;
                vendorfk = Convert.ToInt32(orderdet.VendorFK);
                if (quantity != 0)
                {
                    Orderlist.Add(new OrderModel
                    {
                        PK = Convert.ToInt32(orderdet.PK),
                        VendorFK = Convert.ToInt32(orderdet.VendorFK),
                        NameE = orderdet.NameE,
                        NameC = orderdet.NameC,
                        SalesPrice = orderdet.SalesPrice,
                        DChargeMode = Convert.ToInt32(orderdet.DChargeMode),
                        VatForm = orderdet.VatForm,
                        DeliveryCharge = orderdet.DeliveryCharge,
                        Quantity = quantity,
                        TotalAmount = total,
                        Delete = "delete.png"
                    });                       
                }                    
            }               
        }

        int count = Orderlist.Count;
        for (int i = 0; i < count; i++)
        {
            OrderData.Add(new OrderModel
            {
                PK = Orderlist[i].PK,
                VendorFK = Orderlist[i].VendorFK,
                NameE = Orderlist[i].NameE,
                NameC = Orderlist[i].NameC,
                SalesPrice = Orderlist[i].SalesPrice,
                DChargeMode = Orderlist[i].DChargeMode,
                VatForm = Orderlist[i].VatForm,
                DeliveryCharge = Orderlist[i].DeliveryCharge,
                Quantity = Orderlist[i].Quantity,
                TotalAmount = Orderlist[i].TotalAmount,
                Delete = Orderlist[i].Delete
            });
            if (Orderlist[i].VendorFK == OrderData[0].VendorFK)
            {
                Isexecute = true;
                Issame = true;
            }
            else
            {
                Isexecute = false;
                Issame = false;
            }                
        }
        if(Issame == false)
        {
            await DisplayAlert(LangResource.Alert, "You must order from the same dealer only.", LangResource.ok);
            Isexecute = false;
        }

        if (Isexecute)
        {
            UploadPopUpPage orderPage = new UploadPopUpPage(OrderData);
            orderPage.EventPass += OrderPage_EventPass;
            await PopupNavigation.PushAsync(orderPage);
        }
    }
0
votes

Try This Code

 private async void viewOrderBtn_Clicked(object sender, EventArgs e)
 {
            var item = (Button)sender;  //no idea if this is correct
            var qty = item.BindingContext as ViewModelName or ModelName;


 }