首页 > 解决方案 > xamarin 形式:如何在列表视图中将价格和数量相乘

问题描述

我有列表视图,其中有 qty 和 itemPrice,我需要将这两个相乘并在标签中显示 totalprice。列表视图中的总价标签也是我的代码

<ListView x:Name="mylistview">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout >

                    <Label HorizontalOptions="Center"
                           Text="{Binding Source={x:Reference stepper}, StringFormat='Qty. {0:N}', Path=Value}" 
                           FontSize="11" 
                           TextColor="Black" />

                    <Stepper ValueChanged="stepper_ValueChanged" 
                             Minimum="0" 
                             Maximum="10"         
                             x:Name="stepper" 
                             Value="{Binding Qty}" 
                             Increment="1" 
                             HorizontalOptions="LayoutOptions.Center" 
                             VerticalOptions="LayoutOptions.CenterAndExpand"  />

                    <Label Text="{Binding itemPrice, StringFormat='Unit Price - RS. {0:N}'}" 
                           FontSize="11"
                           TextColor="LightGray" />

                    <Label HorizontalOptions="End"  
                           VerticalOptions="End" 
                           Text="{Binding totalprice}" 
                           FontSize="15"
                           TextColor="#da3043" />

                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

标签: xamarin.forms

解决方案


在您的模型中为 TotalPrice 创建一个只读属性并绑定到它

<Label Text="{Binding TotalPrice}" ... />

public decimal TotalPrice {
  get {
    return itemPrice * Qty;
  }
}

推荐阅读