ListView binding from static property not working

-- -- 892 Reputation points
2021-09-12T20:40:12.717+00:00

Hi

I have a typical ListView-bound-to-an-observable-collection setup (code below). However the observable collection (UpcomingJobsList) is a static property coming from a static class (DataService). The OnAppearing on the page with ListView shows that observable collection is loaded with data (screenshot attached). However no data is shown in the ListView (screenshot attached).

What am I doing wrong?

Thanks

Regards

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"   
             xmlns:models="clr-namespace:BussinessObjects"  
             x:Class="StaffApp.Views.JobsPage"  
             Title="Jobs Page">  
  
    <ContentPage.Content>  
        <StackLayout>  
            <Label Text="Welcome to Jobs Page!"  
                VerticalOptions="CenterAndExpand"   
                HorizontalOptions="CenterAndExpand" />  
  
            <ListView x:Name="JobsListView" >  
                <ListView.ItemTemplate>  
                    <DataTemplate x:DataType="models:UpcomingJobs">  
                        <ViewCell>  
                            <StackLayout Orientation="Vertical">  
                                <Label Text="Welcome to HS Staff Jobs Page 2" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand" TextColor="#6e6d72" />  
                                <Label  Text="Welcome to Jobs Page 3" Padding="0" Margin="0" TextColor="#6e6d72"/>  
  
                                <Grid RowDefinitions="Auto" ColumnDefinitions="47,48,8,52,Auto" Padding="0" Margin="0" ColumnSpacing="0">  
                                    <Label Grid.Column="0" Padding="0" Margin="0" Text="{Binding Date1, StringFormat='{0:dd MMM}'}" HorizontalOptions="Start" VerticalOptions="Center" />  
                                    <Label Grid.Column="1" Padding="0" Margin="0" Text="{Binding From, StringFormat='{0:HH:mm}'}"  HorizontalOptions="End" VerticalOptions="Center" />  
                                    <Label Grid.Column="2" Padding="0" Margin="0" Text="-" HorizontalOptions="Center" VerticalOptions="Center" Style="{StaticResource ExapnderHeaderLabel}" />  
                                    <Label Grid.Column="3" Padding="0" Margin="0" Text="{Binding To1, StringFormat='{0:HH:mm}'}" HorizontalOptions="Start" />  
                                    <Label Grid.Column="4" Padding="0" Margin="0" Text="{Binding Client}" HorizontalOptions="Center" />  
                                </Grid>  
                            </StackLayout>  
                        </ViewCell>  
                    </DataTemplate>  
                </ListView.ItemTemplate>  
            </ListView>  
  
        </StackLayout>  
    </ContentPage.Content>  
</ContentPage>  

131294-jobs-databinding-2.png

131277-screenshot-2021-09-12-21-34-56-6851.jpg

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,314 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 70,811 Reputation points Microsoft Vendor
    2021-09-13T07:29:09.877+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Firstly, please add HasUnevenRows="True" to your <ListView> tag. If records will appear.

    If records still do not show it.

    Please I do not know what is your Viewmodel's code, I make following steps.

    1. Remove x:DataType="models:UpcomingJobs" in the DataTemplate. keep the HasUnevenRows="True" to your <ListView> tag, And add ItemsSource for listview, Here is listview's layout. <StackLayout>
      <Label Text="Welcome to Jobs Page!"
      VerticalOptions="CenterAndExpand"
      HorizontalOptions="CenterAndExpand" />
                 <ListView x:Name="JobsListView" ItemsSource="{Binding JobsList}" HasUnevenRows="True">  
                         <ListView.ItemTemplate>  
                             <DataTemplate >  
                                 <ViewCell>  
                                     <StackLayout Orientation="Vertical">  
                                         <Label Text="Welcome to HS Staff Jobs Page 2" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand" TextColor="#6e6d72" />  
                                         <Label  Text="Welcome to Jobs Page 3" Padding="0" Margin="0" TextColor="#6e6d72"/>  
      
                                         <Grid RowDefinitions="Auto" ColumnDefinitions="47,48,8,52,Auto" Padding="0" Margin="0" ColumnSpacing="0">  
                                             <Label Grid.Column="0" Padding="0" Margin="0" Text="{Binding Date1, StringFormat='{0:dd MMM}'}" HorizontalOptions="Start" VerticalOptions="Center" />  
                                             <Label Grid.Column="1" Padding="0" Margin="0" Text="{Binding From, StringFormat='{0:HH:mm}'}"  HorizontalOptions="End" VerticalOptions="Center" />  
                                             <Label Grid.Column="2" Padding="0" Margin="0" Text="-" HorizontalOptions="Center" VerticalOptions="Center"  />  
                                             <Label Grid.Column="3" Padding="0" Margin="0" Text="{Binding To1, StringFormat='{0:HH:mm}'}" HorizontalOptions="Start" />  
                                             <Label Grid.Column="4" Padding="0" Margin="0" Text="{Binding Client}" HorizontalOptions="Center" />  
                                         </Grid>  
                                     </StackLayout>  
                                 </ViewCell>  
                             </DataTemplate>  
                         </ListView.ItemTemplate>  
                     </ListView>  
      
                 </StackLayout>  
      

    2.I do not know why you changed the BindingContext in the Background, I recomand you to not set two times(bindingContext), set onetime. then binding it.

    Here is layout background code. If you dataSource comes from a static class, I will add these data to the LoadingDataViewModel, In the end I binding it

       public partial class Page1 : ContentPage  
           {  
               public Page1()  
               {  
                   InitializeComponent();  
    
                   LoadingDataViewModel loadingDataViewModel = new LoadingDataViewModel();  
    
    
                   foreach (var item in DataService.UpcomingJobsList)  
                   {  
                       loadingDataViewModel.JobsList.Add(item);  
                   }  
                   BindingContext = loadingDataViewModel;  
    
    
    
               }  
           }  
    

    Here is LoadingDataViewModel.cs Due to I binding the itemsource for listview, So I add an empty ObservableCollection, when we get the data, I will add data to this ObservableCollection. I add my testMyModel.cs and static DataService.cs.

       public class LoadingDataViewModel{  
    
    
               public  ObservableCollection<MyModel> JobsList { get; set; }  
               public LoadingDataViewModel()  
               {  
                   JobsList = new ObservableCollection<MyModel>();  
               }  
           }  
    
    
           public class MyModel  
           {  
               public string Date1 { get; set; }  
    
               public string From { get; set; }  
    
               public string To1 { get; set; }  
    
               public string Client { get; set; }  
           }  
    
    
           public static class DataService  
           {  
               public static ObservableCollection<MyModel> UpcomingJobsList = new ObservableCollection<MyModel>() { new MyModel() { Client="client1", Date1=DateTime.Now.ToString(), From=  DateTime.Now.ToString(), To1= DateTime.Now.ToString() } };  
    
           }  
    

    Here is running screenshot.

    131502-image.png

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful