2016-11-22 173 views
0

我想使用Xamarin窗體有一個菜單(使用ListView)和附近每道菜,有一個步進器和價格的應用程序。Xamarin Forms ListView步進綁定

最後,當用戶完成決定他想要哪些食物和每個食物的數量時,我想要計算總價,但我不知道如何去做。 ListView中

<ListView ItemSelected="OnSelection"> 

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

回答

0

添加項目選定事件後面的代碼:我認爲你需要的Multi-select ListView

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