Sharath
Sharath

Reputation: 2428

Not able to bind to listview in Windows 10 apps (UWP)

I have created sample app for demonstrating the issue. Sorry its quite difficult to put all the code here since there are model classes, datamodel, service file which fetches the data from rest api.

So only few files are being included which gives information.

_placeList = await DataModel.PlaceDataSource.GetData(url); this line of statement from PlacePage.xaml.cs file is actually fetching records but doesn't get binded and displayed in listview.

But gridViewPlaces.ItemsSource = await DataModel.PlaceDataSource.GetData(url); works.

You can find the source code here. Project Download Link

MainPage.xaml

<SplitView x:Name="splitView" IsPaneOpen="True" OpenPaneLength="250" Grid.Row="1" DisplayMode="Inline">
   <SplitView.Pane>
         ...
   </SplitView.Pane>

   <SplitView.Content>
       <Grid>
           <Frame x:Name="rootFrame" />
        </Grid>
  </SplitView.Content>
</SplitView>

PlacePage.xaml

<GridView Name="gridViewPlaces" ItemsSource="{x:Bind PlaceList}" SelectionMode="Single">
    <GridView.ItemTemplate>
         <DataTemplate>
              <Grid Width="200" Height="Auto">
                  <Grid.RowDefinitions>
                      <RowDefinition Height="*" />
                      <RowDefinition Height="*" />
                  </Grid.RowDefinitions>
                  <Grid.ColumnDefinitions>
                       <ColumnDefinition Width="40" />
                       <ColumnDefinition Width="*" />
                  </Grid.ColumnDefinitions>

                  <TextBlock Grid.Row="0" Grid.Column="0" Text="Key" />
                  <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Name}" />
                  <TextBlock Grid.Row="1" Grid.Column="0" Text="Value" />
                  <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Value}" />

              </Grid>
         </DataTemplate>
    </GridView.ItemTemplate>
</GridView>

PagePage.xaml.cs file

private IEnumerable<Place> _placeList;
public IEnumerable<Place> PlaceList
{
     get { return _placeList; }
}
public event EventHandler GroupsLoaded;

protected override void OnNavigatedTo(NavigationEventArgs e)
{
     base.OnNavigatedTo(e);
     url = e.Parameter.ToString();
     LoadPlaces();
}

async private void LoadPlaces()
{
     _placeList = await DataModel.PlaceDataSource.GetData(url);
     //gridViewPlaces.ItemsSource = await DataModel.PlaceDataSource.GetData(url);            // This works
     gridViewPlaces.UpdateLayout();
     if (GroupsLoaded != null)
          GroupsLoaded(this, new EventArgs());
}

Upvotes: 0

Views: 46

Answers (1)

Rob Caplan - MSFT
Rob Caplan - MSFT

Reputation: 21919

Your PlaceList property needs to fire notifications to let the binding know there’s a change. As is, when you replace _placeList you don't notify anybody that PlaceList changed and so nothing updates. The typical pattern here is to initialize the PlaceList property read only and then add things to that existing collection rather than swapping out the collection, though if you notify that you've swapped the collection that should work too.

Additionally, the IEnumerable inside PlaceList needs to provide notifications when its contents change. The standard way to do this is to make it an ObservableCollection since OC implements INotifyPropertyChanged and INotifyCollectionChanged for you. See theBinding to collections Quickstart

Upvotes: 1

Related Questions