Xamarin表单ListView步进绑定 [英] Xamarin Forms ListView Stepper Binding
本文介绍了Xamarin表单ListView步进绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Xamarin Forms制作一个具有菜单(使用ListView)的应用程序,并且在每道菜附近都有步进器和价格.
I am trying to make an application using Xamarin Forms which has a menu (using ListView) and near each dish, there is a stepper and price.
最后,当用户确定自己想要的餐点以及每个餐点的数量时,我想计算总价,但我不知道该怎么做.
In the end when a user finished with deciding which meals he want and how many of each, I want to count Total Price but I have no idea how to do it.
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Eba_face.MenuPage"
Title="Menu">
<Grid VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<ListView x:Name="listview"
HasUnevenRows="True"
VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal" Padding="5">
<StackLayout Orientation="Vertical">
<Image x:Name="image" Source="{Binding Image}" WidthRequest="75" HeightRequest="75"/>
<Label Text="{Binding Name}" FontAttributes="Bold"/>
</StackLayout>
<StackLayout Orientation="Vertical">
<Label Text="{Binding Weight, StringFormat='Weight {0}'}"/>
<Label Text="{Binding Price}" TextColor="Gray"/>
</StackLayout>
<StackLayout HorizontalOptions="EndAndExpand">
<Stepper x:Name="stepper" ValueChanged="Handle_StepperValueChanged"/>
<Label Text="{Binding Source={x:Reference stepper}, Path=Value}" HorizontalOptions="Center"/>
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<StackLayout Orientation="Vertical" VerticalOptions="End" HorizontalOptions="Center">
<Label x:Name="totalPriceLabel" Text="Total Price:" HorizontalOptions="Center"/>
<Button Text="Proceed to finish the order" />
</StackLayout>
</Grid>
</ContentPage>
推荐答案
在ListView中添加项目选定事件
Add item selected event in ListView
<ListView ItemSelected="OnSelection">
在后面的代码中:
void OnSelection (object sender, SelectedItemChangedEventArgs e)
{
if (e.SelectedItem == null)
{
return;
}
var yourBindindingObject= (listView.ItemsSource as List<YourBindindingObject>).IndexOf(e.SelectedItem as YourBindindingObject);
int price = yourBindindingObject.Price;
AllPrice +=price;
}
但我认为您需要 多选 ListView
这篇关于Xamarin表单ListView步进绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文