无法将 xaml 文件中使用的自定义步进器的文本 属性 获取到 xamarin 表单中的 cs 文件
Not able to get the text property of custom stepper used in the xaml file to its cs file in xamarin forms
到这里就完成了群组绑定,产品列在对应的经销商下。我想将一个经销商下每个产品的价格和数量相乘,并在单击 View Order
按钮时将其存储在 List_model-OrderModel
中。我无法访问我的 cs 文件中自定义步进器文本 中的 数量值。
我试过了var qty = stepper.Text
。但是名字 stepper
不会出现。 CustomStepper
和 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++;
}
}
这是我的模型class代码:
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; }
}
我在我的按钮中有这个点击相应的cs文件:
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>();
}
我的 xaml 文件:
<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>
图片:
My screen
更新:
我附上了调试的图片 window。请带我离开这里。我可以从 xaml 中获取数据。我是 xamarin 表单的新手。不知道如何从这里开始。
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
试试这个代码
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;
}
@chetan 感谢您花时间和耐心帮助我。
这是最终代码。
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);
}
}
到这里就完成了群组绑定,产品列在对应的经销商下。我想将一个经销商下每个产品的价格和数量相乘,并在单击 View Order
按钮时将其存储在 List_model-OrderModel
中。我无法访问我的 cs 文件中自定义步进器文本 中的 数量值。
我试过了var qty = stepper.Text
。但是名字 stepper
不会出现。 CustomStepper
和 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++;
}
}
这是我的模型class代码:
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; }
}
我在我的按钮中有这个点击相应的cs文件:
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>();
}
我的 xaml 文件:
<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>
图片: My screen
更新: 我附上了调试的图片 window。请带我离开这里。我可以从 xaml 中获取数据。我是 xamarin 表单的新手。不知道如何从这里开始。
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
试试这个代码
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;
}
@chetan 感谢您花时间和耐心帮助我。
这是最终代码。
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);
}
}