Xamarin表单ListView步进绑定

时间:2016-11-22 05:59:19

标签: xaml listview xamarin xamarin.forms uistepper

我正在尝试使用具有菜单(使用ListView)的Xamarin Forms创建应用程序,并且在每个菜附近,有一个步进器和价格。

最后当一个用户决定了他想要的餐点以及每个餐点的数量时,我想计算总价格,但我不知道该怎么做。

    <?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>

1 个答案:

答案 0 :(得分:0)

在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;
}

但我认为你需要Multi-select ListView