在列表视图中单击其他按钮时更改按钮的图像,查看单元格 [英] Change a button's image on tap of other button inside a list view, view cell

查看:70
本文介绍了在列表视图中单击其他按钮时更改按钮的图像,查看单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a listview. This list view has 5 rows with two buttons namely A & B. When I tap on button A on a row, I want to change the image on A as well as B on the same row and vice-versa. I am able to individually tap and change the image on the same button but don't know how to change the image on the other button. Here is my 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并更改按钮的图像.

I am then using the sender to identify the class ID and change the image of the button.

我应该使用command吗?我还应该做点别的吗?请帮忙.谢谢

Should I be using a command? Should I be doing something else? Please help. Thanks

推荐答案

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

If you want to use the CommandParameter you should be using a Command. At the moment you're mixing 2 different ways to handle the click.

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

One thing to note though is that if you use a command you usually want to bind it to a Command that is defined on your ViewModel but since inside the DataTemplate your BindingContext is the ListView item instead of the ViewModel so you have to reference around that. Something like this should work:

<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上进行搜索,您只需将其直接插入命令中即可!

Note here that you have to give your ContentPage an x:Name (so you can reference it and call the BindingContext on it). And the "{Binding .}" binds to the current list item. So no need to search for it on id you can just plug it into the command directly!

这篇关于在列表视图中单击其他按钮时更改按钮的图像,查看单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆