传递命令参数以删除列表视图条目

时间:2018-08-15 11:04:06

标签: xamarin xamarin.forms

我有一个菜单项,可以删除ListView中的条目。我的问题是我传递的命令参数始终为空。

列表视图`                                              

                        <StackLayout Orientation="Horizontal">
                            <Image Source="{Binding Path=BindingContext.ContactImage, Source={x:Reference contactsListView}}" VerticalOptions="Center" Margin="15,0,0,0"></Image>
                            <Label Text="{Binding FullName}" VerticalOptions="Center"></Label>
                            <Image Source="{Binding Favorite, Converter={StaticResource FavoriteImageConverter}}" VerticalOptions="Center" HorizontalOptions="EndAndExpand">
                                <Image.GestureRecognizers>
                                    <TapGestureRecognizer
                                        Command="{Binding BindingContext.TapCommand, Source={x:Reference contactsListView}}" 
                                        CommandParameter="{Binding FullName}"/>

                                </Image.GestureRecognizers>
                            </Image>
                            <Image Source="{Binding Path=BindingContext.PhoneImage, Source={x:Reference contactsListView}}" VerticalOptions="Center" HorizontalOptions="End" Margin="0,0,15,0">
                                <Image.GestureRecognizers>
                                    <TapGestureRecognizer
                                        Command="{Binding BindingContext.CallCommand, Source={x:Reference contactsListView}}" 
                                        CommandParameter="{Binding ContactNumber}"/>
                                </Image.GestureRecognizers>
                            </Image>

                        </StackLayout>

                        <ViewCell.ContextActions>
                            <MenuItem  Text="Edit" IsDestructive="False" Command="{Binding Source={x:Reference contactsListView}, Path=BindingContext.EditCommand}" BindingContext="{Binding Source={x:Reference contactsListView}, Path=BindingContext.EditMenu}"/>
                            <MenuItem  Text="Delete" IsDestructive="True" 
                                       Command="{Binding Source={x:Reference contactsListView}, Path=BindingContext.DeleteCommand}" 
                                       CommandParameter="{Binding FullName}" 
                                       BindingContext="{Binding Source={x:Reference contactsListView}, Path=BindingContext.DeleteMenu}"/>
                        </ViewCell.ContextActions>


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

如果我将CommandParameter更改为CommandParameter = "test", the test string is properly passed。这是什么问题?

2 个答案:

答案 0 :(得分:1)

您正在使用x定义删除MenuItem的XAML

<MenuItem  
    Text="Delete" 
    IsDestructive="True" 
    Command="{Binding Source={x:Reference contactsListView}, Path=BindingContext.DeleteCommand}" 
    CommandParameter="{Binding FullName}" 
    BindingContext="{Binding Source={x:Reference contactsListView}, Path=BindingContext.DeleteMenu}" />

由于它位于您的DataTemplate中,因此BindingContext的默认ViewCell是与列表项关联的单个Contact,其属性为{{1 }}。

无论如何,您是在显式设置菜单项的FullName。由于BindingContext已绑定到CommandParameter的{​​{1}},因此XF将在您的BindingContext更改时尝试更新MenuItem。但是由于CommandParameter没有属性BindingContext,因此DeleteMenu将是FullName。如果您未明确设置CommandParameter,它应该可以工作,因为在这种情况下,null是父项之一,即您的BindingContext

答案 1 :(得分:1)

假设您的ListView中已填充了一组视图模型,则可以通过以下方法引用从中调用MenuItem Command的模型:

tensorflow 1.4

“”。是listview项源中当前项的缩写(只需将圆点留在原处。看起来不对,但可行)。