在列表视图中单击其他按钮时更改按钮的图像,查看单元格

时间:2018-12-07 12:48:10

标签: c# xamarin xamarin.forms

我有一个listview。此list view具有5行,其中两个buttonsAB。当我点击button A上的row时,我想更改A上的图像以及同一行上的B,反之亦然。我能够单独点击并更改同一button上的图像,但是不知道如何更改另一个按钮上的图像。这是我的listview

<ListView x:Name="GroupedView" SeparatorColor="Transparent" GroupDisplayBinding="{Binding Title}" IsGroupingEnabled="true" HasUnevenRows="true" >
                    <ListView.GroupHeaderTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <StackLayout Orientation="Horizontal" Padding="5" BackgroundColor="#E2F5F9">
                                    <Label Text="{Binding Title}" TextColor="{StaticResource NavyBlue}" />
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.GroupHeaderTemplate>
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <StackLayout Orientation="Horizontal" Spacing="2" Padding="5">
                                    <StackLayout VerticalOptions="FillAndExpand">
                                        <Label Text="{Binding QuestionName}" />
                                    </StackLayout>
                                    <StackLayout IsVisible="{Binding ShowYesNo}" Spacing="15" Orientation="Horizontal" HorizontalOptions="End">
                                        <Button ClassId="Yes" Clicked="ChoiceSelected" CommandParameter="{Binding question_id}" Image="{Binding YesChoiceImg}" />
                                        <Button ClassId="No" Clicked="ChoiceSelected" CommandParameter="{Binding question_id}" Image="{Binding NoChoiceImg}" />
                                    </StackLayout>
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

然后,我使用发件人识别class ID并更改按钮的图像。

我应该使用command吗?我应该做别的事情吗?请帮忙。谢谢

1 个答案:

答案 0 :(得分:0)

如果要使用CommandParameter,则应使用Command。目前,您正在混合两种不同的方式来处理点击。

但是要注意的一件事是,如果您使用命令,通常希望将其绑定到ViewModel上定义的Command,但是由于在DataTemplate内部,您的BindingContext是ListView项而不是ViewModel,因此您必须引用在那附近。这样的事情应该起作用:

<DataTemplate>
    <ViewCell>
        <StackLayout Orientation="Horizontal" Spacing="2" Padding="5">
            <StackLayout VerticalOptions="FillAndExpand">
                <Label Text="{Binding QuestionName}" />
            </StackLayout>
            <StackLayout IsVisible="{Binding ShowYesNo}" Spacing="15" Orientation="Horizontal" HorizontalOptions="End">
                <Button ClassId="Yes" Command="{Binding Path=BindingContext.ButtonCommand, Source={x:Reference MyQuestionPage}}" CommandParameter="{Binding .} Image="{Binding YesChoiceImg}" />
                <Button ClassId="No" Clicked="ChoiceSelected" CommandParameter="{Binding question_id}" Image="{Binding NoChoiceImg}" />
            </StackLayout>
        </StackLayout>
    </ViewCell>
</DataTemplate>

请注意,您必须为ContentPage指定一个x:Name(以便您可以引用它并在其上调用BindingContext)。并且"{Binding .}"绑定到当前列表项。因此,无需在id上进行搜索,您只需将其直接插入命令中即可!