【问题标题】:Xamarin Forms ListView Stepper BindingXamarin 表单 ListView 步进器绑定
【发布时间】:2017-04-05 17:04:07
【问题描述】:

我正在尝试使用 Xamarin Forms 制作一个应用程序,该应用程序有一个菜单(使用 ListView),并且在每道菜附近,都有一个步进器和价格。

最后,当用户完成决定他想要哪些餐点以及每餐多少餐时,我想计算总价格,但我不知道该怎么做。

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

【问题讨论】:

    标签: xaml listview xamarin xamarin.forms uistepper


    【解决方案1】:

    在ListView中添加item选中事件

    <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

    【讨论】:

      猜你喜欢
      • 2016-10-10
      • 1970-01-01
      • 2016-09-08
      • 2021-10-20
      • 1970-01-01
      • 2021-11-08
      • 2017-10-11
      • 2017-04-16
      • 2015-11-05
      相关资源
      最近更新 更多