0

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.

6
  • Where is this array of strings? in your ViewModel or Xaml.cs? Commented Jan 21, 2020 at 10:49
  • array of strings placed in View model Commented Jan 21, 2020 at 10:55
  • Can you show me your VM code quickly Commented Jan 21, 2020 at 10:55
  • I provide my VM. Please check the code. Commented Jan 21, 2020 at 10:56
  • Did the below solution work for you! Commented Jan 21, 2020 at 11:25

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Did you modify the ItemSource ItemsSource="{Binding runningLateOption}" ?
You need to implement INotifyPropertyChanged in the ViewModel, you can do that by using MVVMHelpers Nuget Package and use it as properties are declared in ViewModel
1

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>

3 Comments

You could share a sample so that I can test it on my side .
I provided the Listview and View Model above. I not using except that. Just that array is not binding to the listview.
Provide the screen shot of the effect on your device .
1

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

1 Comment

Don't forget to accept your answer , which will help more people .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.