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

Simple example of customizing an item in a listview?

$
0
0

I have a simple listview with an image in the last column. I would like to simply change the image source based on the value of a data field.
For example, if orderstatus = 1, show "icon1.png" image, if orderstatus = 2, show "icon2.png", etc.

I know I could just ouput my data with the correct image name and bind to it, but is there another way to do this? Is there an "itemdatabound" event that I can switch the image in the code? Or is there a simple example of customizing a grid inside a viewcell that I could look at?

Thanks for any help!!

    <ListView
        x:Name="listViewOrders"
        ItemsSource="{Binding Orders}"
        Refreshing="listViewOrders_Refreshing"
        ItemTapped="listViewOrders_ItemTapped"
        BackgroundColor="{StaticResource listBGColor}"
        SeparatorColor="{StaticResource separatorColor}"
        IsPullToRefreshEnabled="True"
        IsVisible="False"
        RowHeight="50">
      <ListView.ItemTemplate>
        <DataTemplate>
          <ViewCell>
            <ViewCell.View>
              <Grid Padding="0" RowSpacing="0" ColumnSpacing="0" >
                <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="60" />
                  <ColumnDefinition Width="*" />
                  <ColumnDefinition Width="30" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                  <RowDefinition Height="Auto" />
                  <RowDefinition Height="Auto" />
                  <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Label Text="{Binding OrderNumber}" Grid.Column="0" Grid.Row="0" FontSize="14" LineBreakMode="NoWrap"></Label>
                <Label Text="{Binding Name}" Grid.Column="1" Grid.Row="0" FontSize="14" LineBreakMode="NoWrap"></Label>
                <Label Text="{Binding CustomerNumber}" Grid.Column="1" Grid.Row="1" FontSize="12" TextColor="Gray" LineBreakMode="NoWrap"></Label>
                <Label Text="{Binding OrderDate, StringFormat='{0:g}'}" Grid.Column="1" Grid.Row="2" FontSize="12" TextColor="Gray" LineBreakMode="NoWrap"></Label>
                <Image Source="icon_checked.png" Grid.Column="2" Grid.Row="0" WidthRequest="25" HeightRequest="25"></Image>
              </Grid>
            </ViewCell.View>
          </ViewCell>
        </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>

Viewing all articles
Browse latest Browse all 89864

Trending Articles