Is it possible to set an items visibility depending if list view item is selected

JKayJKay USMember ✭✭✭
edited October 2015 in Xamarin.Forms

I have a listview like so:

 <ListView x:Name="listView" ItemsSource="{Binding Customers}" SelectedItem="{Binding SelectedCustomer}">
                <ListView.ItemTemplate>
                  <DataTemplate>
                    <ViewCell>
                      <ViewCell.View>
                        <Grid>

                          <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="30"/>
                          </Grid.ColumnDefinitions>

                          <StackLayout Grid.Column="0">
                            <Label Text="{Binding Name}"  FontAttributes="Bold"/>
                            <Label Text="{Binding Address, Converter={StaticResource AddressToStringConverter}, ConverterParameter={StaticResource SortAdressLength}}" />
                          </StackLayout>
                            <Image Grid.Column="1" Source="Info.png"/> <!-- Visibility depending on if selected -->
                        </Grid>
                      </ViewCell.View>
                    </ViewCell>
                  </DataTemplate>
                </ListView.ItemTemplate>
              </ListView>

Now I would like to set the Image "Info.png" to only be visible on whether the ListViewItem is selected. Now in WPF I would add a ListViewItemContainerStyle with a Trigger dependent on whether the ListViewItem was selected and set the Visibility.

I also know that I could have a property on my customer object called IsSelected and use that. But maintaining a property called IsSelected Seems very scrappy to me?

Is there a better way?

In addition to this I would also like to set my Selected Item background using a similar technique

Answers

  • AdamMeaneyAdamMeaney USMember ✭✭✭✭

    Xamarin Forms has Triggers and Behaviors. One of those will do what you want, I can just never remember which does what.

  • Quan.HoangQuan.Hoang USMember

    I would set the IsVisible to true or false accordingly on the listview ItemSelected

Sign In or Register to comment.