Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 89864

How to show image when user click on collection view

$
0
0

I have a Collection view with a label and an image. I want to make the elements to have a checkmark when clicked (instead of the default behavior, which is to change the background color).

With the code below, when the user clicks on a line, the background color is changed to gray. However, the image with a checkmark is not showed, even though I have defined a setter for the image.

```xml

<ContentPage.Resources>
        <ResourceDictionary>
            <Style TargetType="Grid">
                <Setter Property="VisualStateManager.VisualStateGroups">
                    <VisualStateGroupList>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal" />
                            <VisualState x:Name="Selected">
                                <VisualState.Setters>
                                    <Setter Property="BackgroundColor" Value="LightGray" />
                                    <Setter x:DataType="Image" Property="Image.Source" Value="icon_check.png" />
                                </VisualState.Setters>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateGroupList>
                </Setter>
            </Style>            
        </ResourceDictionary>
    </ContentPage.Resources>


//

<CollectionView 
                                x:Name="collectionview_cadastronotificacao_tipoocorrencia"
                                ItemsSource="{Binding TipoOcorrencia}" 

                                SelectionMode="Multiple" 
                                BackgroundColor="White" 
                                HorizontalOptions="Center" 
                                VerticalOptions="Center"
                                SelectionChanged="CollectionView_SelectionChanged" >

                    <CollectionView.ItemTemplate>
                        <DataTemplate>
                            <Grid Padding="10" >

                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*" />                                    
                                </Grid.RowDefinitions>

                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="Auto" />                                    
                                </Grid.ColumnDefinitions>

                                <Image 
                                   Grid.RowSpan="1"
                                   Grid.Column="2" />

                                <Label Grid.Column="0"
                                       Text="{Binding Descricao}"
                                       FontAttributes="Bold" FontSize="Small"/>

                            </Grid>
                        </DataTemplate>
                    </CollectionView.ItemTemplate>
                </CollectionView>
```

If I change <Setter x:DataType="Image" Property="Image.Source" to <Setter x:DataType="Image" Property="Source", an InvalidCastException is thrown.


Viewing all articles
Browse latest Browse all 89864

Trending Articles