Priyanka
Priyanka

Reputation: 158

Array of string not binding to the listview

I'm trying to bind an array to list view. It's not binding and showing blank. I used Model class to place array.

View Model:

public class RunningLateOptions
    {
      public string[] runningLateOptions = new[] { "30 Mins", "1 Hour", "1:30 Hour", "2 Hours" };
        public string[] runningLateOption
        {
           get{ return runningLateOptions; }
        }
    }

XAML code:

<ListView x:Name="RunningLateOptions" ItemsSource="{Binding RunningLateOptions}" ItemSelected="runningLateSelectedItem">
     <ListView.ItemTemplate>
        <DataTemplate>
          <Label x:Name="listItems" Text="{Binding runningLateOption}" HorizontalTextAlignment="Center"/>
        </DataTemplate>
     </ListView.ItemTemplate>
</ListView>

I'm not able to understand what's wrong with this. Help me.

Upvotes: 0

Views: 1854

Answers (3)

Priyanka
Priyanka

Reputation: 158

I gave the array of strings in the CS file and bind to listview by using itemsource. I didn't use any View model here.

CS code:

string[] runningLateOptions = { "30 Mins", "1 Hour", "1:30 Hour", "2 Hours"  };
RunningLateOptions.ItemsSource = runningLateOptions;

XAML code:

<ListView x:Name="RunningLateOptions" ItemsSource="{Binding Items}" ItemSelected="runningLateSelectedItem">
   <ListView.ItemTemplate>
     <DataTemplate>
        <ViewCell>
          <Label BackgroundColor="Transparent" x:Name="listItems" Text="{Binding}" TextColor="Black" HorizontalOptions="Center"></Label>
         </ViewCell>
     </DataTemplate>
   </ListView.ItemTemplate>
</ListView>

TextCell not providing the HorizontalTextAlignment attribute. That's why I used view cell for the label.

Thanks for helping me. Click here to see the output

Upvotes: 1

Lucas Zhang
Lucas Zhang

Reputation: 18861

The type ItemsSource of ListView or CollectionView should be a list which implement the Interface IEnumerable .

So we normally set it as an ObservableCollection<T> or List<T> .

In your case , you could set the ItemsSource of the ListView like

public ObservableCollection<string> RunningLateOptions {get; set;}

ObservableCollection has implemented the Interface INotifyPropertyChanged. So you don't need to implement it any more .

For more details about ListView you could refer https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/listview/data-and-databinding

<ListView x:Name="RunningLateOptions" ItemsSource="{Binding RunningLateOptions}" ItemSelected="runningLateSelectedItem">
   <ListView.ItemTemplate>
       <DataTemplate>
           <StackLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
              <Label x:Name="listItems" Text="{Binding runningLateOption}" HorizontalTextAlignment="Center"/>
           </StackLayout>
       </DataTemplate>
   </ListView.ItemTemplate>
</ListView>

Upvotes: 1

Nikhil
Nikhil

Reputation: 3387

You need to modify the xaml as follows:-

<ListView x:Name="RunningLateOptions" ItemsSource="{Binding runningLateOption}" ItemSelected="runningLateSelectedItem">
     <ListView.ItemTemplate>
        <DataTemplate>
          <Label x:Name="listItems" Text="{Binding .}" HorizontalTextAlignment="Center"/>
        </DataTemplate>
     </ListView.ItemTemplate>
</ListView>

Let me know if you face more difficulties.

Upvotes: 2

Related Questions