ListView not displaying contents of bindingContext

I have this layout in my ContentView

 <ContentView.Content>
        <xForms:SfListView x:Name="OrderHistoryListView" ItemsSource="{Binding OrderHistory}"
                           ItemSize="100">
            <xForms:SfListView.ItemTemplate>
                <DataTemplate>
                    <Grid Padding="10">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="0.4*" />
                            <RowDefinition Height="0.6*" />
                        </Grid.RowDefinitions>
                        <Label Text="{Binding LocationName}" FontAttributes="Bold" TextColor="Teal" FontSize="21" />
                        <Label Grid.Row="1" Text="{Binding OrderDate}" TextColor="Teal" FontSize="15"/>
                    </Grid>
                </DataTemplate>
            </xForms:SfListView.ItemTemplate>
        </xForms:SfListView>
    </ContentView.Content>

and in the code behind I set the BindingContext

public OrderHistoryView (OrderHistoryViewModel viewModel)
{
InitializeComponent ();

    BindingContext = viewModel;

}

but when i run the app, the list never shows any content.



5 Replies

MK Muthu Kumaran Gnanavinayagam Syncfusion Team November 21, 2017 06:17 PM UTC

Hi Christian, 
 
We have checked with the reported query “ListView not displaying contents of BindingContext” and referred the attached code snippet from our side. We would like to let you know that you have not created the ViewModel instance before assigning it to BindingContext. Thus the items are not added into collection and list is shown empty. You need to create instance for ViewModel as like below code example. 
 
Code Example[C#]: 
public OrderHistoryView(OrderHistoryViewModel viewModel) 
{ 
  InitializeComponent(); 
  viewModel = new OrderHistoryViewModel(); 
  BindingContext = viewModel; 
} 
 
Please let us know if you require further assistance. 
 
Regards, 
G.Muthu Kumaran. 



CG christian Green November 22, 2017 01:26 AM UTC

Thank you for the reply.  But the view model is created and then injected into the view.

  var viewModel=new OrderHistoryViewModel();
            viewModel.Populate();

            ContentView.Children.Add(new OrderHistoryView(viewModel));

this code creates the orderhistoryview which takes in the orderhistoryVM and is what calls this 

public OrderHistoryView (OrderHistoryViewModel viewModel)
{
InitializeComponent ();

    BindingContext = viewModel;

}

so the viewmodel already exists when the c'tor is called, see below



there are 3 items in the collection


MK Muthu Kumaran Gnanavinayagam Syncfusion Team November 23, 2017 12:49 PM UTC

Hi Christian, 

We have checked the reported query “SfListView items does not render in the view when Loaded inside ContentView” from our side. Unfortunately, we could not reproduce the reported issue from our side. In our sample we have loaded the SfListView inside a ContentPage(MainPage) and navigated to ContentView where you can see the SelectedItems of SfListView from MainPage. The SfListView items are rendered in the view as expected.  

For you reference, we have attached the sample link below. 


Could you please check whether the reported issue occurs in our sample also? If so, please share the details of the tested device. 
If the attached sample does not meet your requirements, Please share the sample which replicated the reported issue or modify the attached sample to reproduce the reported issue which helps us to analyze the issue better. Also could you please check the following details from your side? 

  • Whether the ItemsSource is bound properly to SfListView and contains the items collection?
  • Whether the reported issue occurs in particular version of Xamarin.Forms and SfListView?(We have checked with X.Forms(v.2.4.0.280) and SfListView(v.15.4.0.17))
  • Whether the reported issue occurs in that particular device or all other devices?(We have in Moto G4(Android v 7.0))

Regards, 
G.Muthu Kumaran. 



CG christian Green November 24, 2017 02:54 PM UTC

I created a sample.  

I realized and proved this in my real app also.  The listview works on a ContentPage.  But not an on ContentView.  In the sample, I changed the start page to Page1 and then changed the MainPage to ContentView and now the listview isn't being shown.  

The button that was at the bottom of the page is now floating around the header of the listview, so for some reason, it looks like its visibility or height issue with contentviews.

I have attached the updated sample.

Attachment: ListViewSample_138a5d1d.7z

the other difference is that instead of the shared project for the PCL, I use a. net Standard 2.0 for the shared porject.


MK Muthu Kumaran Gnanavinayagam Syncfusion Team November 27, 2017 01:41 PM UTC

Hi Christian, 
 
We have checked the reported query “SfListView height does not set properly when loaded in ContentView” and also referred the attached sample from our side. While loading the SfListView inside ContentPage, it acquires the size of the whole ContentPage. But when loaded inside ContentView, we need to mention the Height of SfListView inside the View. So you can set the parent element Height to SfListView’s HeightRequest to overcome the issue. 
 
Note: We have already considered it as an enhancement from our side and the date of implementation is tentative. 
 
Please let us know if you require further assistance. 
 
Regards, 
G.Muthu Kumaran. 


Loader.
Up arrow icon