我有一个listview
. This list view
有 5 行,其中有 2 行buttons
namely A
& B
。当我点击button A
on 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
?我应该做点别的事吗?请帮忙。谢谢
如果您想使用 CommandParameter,则应该使用 Command。目前,您正在混合两种不同的方式来处理点击声。
但需要注意的一件事是,如果您使用命令,您通常希望将其绑定到 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 上搜索它,你可以直接将它插入命令中!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)