C# xamarin表单-如何在';删除';按钮点击
“删除”按钮添加到listview上的每个项目中,当用户单击“删除”按钮时,它将调用RemoveItemFromShoppingList()并从购物车列表中删除产品 我已经测试了调用RemoveItemFromShoppingList()并从购物列表中删除第一项。这一切工作正常,是新的购物车列表更新 但是,我不想删除第一项,而是要删除用户单击的产品。我已尝试捕获ProductId,但无法获取它…请提供建议C# xamarin表单-如何在';删除';按钮点击,c#,xaml,xamarin,xamarin.forms,C#,Xaml,Xamarin,Xamarin.forms,“删除”按钮添加到listview上的每个项目中,当用户单击“删除”按钮时,它将调用RemoveItemFromShoppingList()并从购物车列表中删除产品 我已经测试了调用RemoveItemFromShoppingList()并从购物列表中删除第一项。这一切工作正常,是新的购物车列表更新 但是,我不想删除第一项,而是要删除用户单击的产品。我已尝试捕获ProductId,但无法获取它…请提供建议 <ContentPage.Content> <S
<ContentPage.Content>
<StackLayout>
<ListView ItemsSource="{Binding ShoppingListItemSource}" HasUnevenRows="True" SeparatorVisibility="None">
<ListView.Footer>
<Label x:Name="Total" HorizontalTextAlignment="End" VerticalTextAlignment="Start" Margin="20,20" FontAttributes="Bold"/>
</ListView.Footer>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Padding="10" RowSpacing="10" ColumnSpacing="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Text="{Binding ProductId}" VerticalOptions="End" IsVisible="False"/>
<controls:CircleImage Grid.Column="1" Grid.Row="1" HeightRequest="60" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand" Aspect="AspectFill" WidthRequest="66" Grid.RowSpan="2" Source="{Binding Img}"/>
<Label Grid.Column="2" Grid.Row="1" Text="{Binding Name}" VerticalOptions="End"/>
<Label Grid.Column="2" Grid.Row="2" VerticalOptions="Start" Text="{Binding Detail}"/>
<Label Grid.Column="3" Grid.Row="2" VerticalOptions="Start" Text="{Binding TotalPrice, StringFormat='£{0:0.00}'}"/>
<Label Grid.Column="4" Grid.Row="2" VerticalOptions="Start" Text="{Binding QuantityOfProduct}"/>
<Label Grid.Column="5" Grid.Row="2" VerticalOptions="Start" Text="{Binding SubTotal, StringFormat='£{0:0.00}'}"/>
<Button Grid.Column="6" Grid.Row="2" VerticalOptions="Start" Text="Delete" Clicked="RemoveItemFromShoppingList" Grid.RowSpan="2" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage.Content>
</ContentPage>
使用
BindingContext
-这假设您的ItemsSource
是类型产品的集合
void RemoveItemFromShoppingList(object sender, EventArgs e)
{
var button = (Button)sender;
var item = (Product)button.BindingContext;
...
}
使用
BindingContext
-这假设您的ItemsSource
是类型产品的集合
void RemoveItemFromShoppingList(object sender, EventArgs e)
{
var button = (Button)sender;
var item = (Product)button.BindingContext;
...
}
我认为在这些类型的场景中建议使用命令而不是事件处理程序我认为在这些类型的场景中建议使用命令而不是事件处理程序