单击按钮时如何获取itemlistview详细信息

时间:2019-06-16 10:36:39

标签: xamarin.forms

单击“添加到购物车”按钮时,我希望将单击的行添加到本地数据库。

我尝试实现clicked选项,但显示System.InvalidCastException:指定的强制转换无效。

我的Xaml是:

 <controls:FlowListView x:Name="gallery"
                           FlowColumnCount="2"
                           SeparatorVisibility="Default"
                           HasUnevenRows="True"
                           FlowItemsSource="{Binding ItemsGallery}"
                           FlowUseAbsoluteLayoutInternally="True"
                           FlowItemTapped="OnFlowItemTapped"
                           FlowColumnExpand="Proportional"

                           BackgroundColor="White">
        <controls:FlowListView.FlowColumnTemplate>
            <DataTemplate>
                <StackLayout>
                    <Frame BorderColor="#DCDCDC" HasShadow="True" Margin="2,2,2,2" CornerRadius="5">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="100"/>
                                <RowDefinition Height="35"/>
                                <RowDefinition Height="20"/>
                                <RowDefinition Height="20"/>
                                <RowDefinition Height="35"/>
                            </Grid.RowDefinitions>
                            <ffimageloading:CachedImage Source="{Binding image}" Grid.Row="0" LoadingPlaceholder="ItemsGallery" HeightRequest="120" WidthRequest="120" />
                        <Label Grid.Row="1"
                           VerticalOptions="End"
                           FontAttributes="Bold"
                           FontSize="Medium"
                           HorizontalTextAlignment="Start"

                               TextColor="Black"
                           Text="{Binding name}" />

                            <Label Grid.Row="2" HorizontalOptions="Start"
                           VerticalOptions="End"
                           FontAttributes="Bold"
                           FontSize="Medium"
                           Text="{Binding weight}" />

                            <Label HorizontalOptions="Start" Grid.Row="3" 
                           VerticalOptions="End"
                           FontAttributes="Bold"
                           FontSize="Medium"
                           Margin="2"
                           Text="{Binding price}" />
                            <Button Text="Add TO Cart"  Grid.Row="4" BackgroundColor="Coral" TextColor="WhiteSmoke" Clicked="Button_Clicked_1" />
                        </Grid>
                    </Frame>
                </StackLayout>
            </DataTemplate>
        </controls:FlowListView.FlowColumnTemplate>
    </controls:FlowListView>

我的逻辑是:

私有异步void Button_Clicked_1(对象发送者,EventArgs e)     {

    if (((ListView)sender).SelectedItem == null)
        return;

    var myList = (ListView)sender;
    var myProuct= (myList.SelectedItem as ProductDetail);


}

2 个答案:

答案 0 :(得分:0)

Button Click事件的发件人将是按钮,而不是包含按钮的ListView

private async void Button_Clicked_1(object sender, EventArgs e) {

    // the sender of a button click event is a BUTTON
    var btn = (Button)sender;

    var myProduct = (ProductDetail)btn.BindingContext;
}

答案 1 :(得分:0)

您可以使用带有viewmodel的常规数据绑定方式,也可以尝试

Button Text="Add TO Cart"  Grid.Row="4" BackgroundColor="Coral" TextColor="WhiteSmoke" Command="{Binding AddCommand}" CommandParameter = "{Binding .}" 

但是随后您需要编辑建立ItemsGallery列表的模型。

public ICommand AddCommand { get; set; }

在创建对象时

new Gallery
{
           //your stuff
           AddCommand = new Command<Gallery>(async (item) => await 
           AddItem(item))
})



 private async Task AddItem(Gallery item)
 {
        //your implementation
 }