【问题标题】:Change a button's image on tap of other button inside a list view, view cell通过点击列表视图中的其他按钮更改按钮的图像,查看单元格
【发布时间】:2018-12-07 12:48:10
【问题描述】:

我有一个listview。这个list view 有 5 行,其中有两个buttons,即AB。当我在row 上点击button A 时,我想更改同一行上AB 上的图像,反之亦然。我可以单独点击并更改同一 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 吗?我应该做点别的吗?请帮忙。谢谢

【问题讨论】:

  • 请贴出ChoiceSelected的代码
  • @Jason- 这里是代码:var item = sender as Button; if (item.ClassId.Equals("Yes")) { string source = item.Image as FileImageSource; if (source.Equals("UnCheck.png")) { item.Image = "Check.png"; } else { item.Image = "UnCheck.png"; } } else ....

标签: c# xamarin xamarin.forms


【解决方案1】:

如果你想使用 CommandParameter,你应该使用 Command。目前,您正在混合使用 2 种不同的方式来处理点击。

需要注意的一点是,如果您使用命令,您通常希望将其绑定到在 ViewModel 上定义的命令,但由于在 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上搜索,直接插入命令即可!

【讨论】:

  • 谢谢@Knoop - 当我使用Command 单击按钮时,我可以更改图像,但是,listview 中也有类似的按钮。当我更改一个按钮的图像时,同一列表中的另一个按钮也会更新。如何确保图像更改为我单击的特定行?我要写linq 吗?
  • 很高兴听到您成功了!祝项目顺利
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-17
  • 2018-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多