简体   繁体   English

无法绑定到Windows 10应用程序(UWP)中的列表视图

[英]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. 抱歉,将所有代码放在这里非常困难,因为这里有模型类,数据模型和服务文件,这些文件从rest api获取数据。

So only few files are being included which gives information. 因此,仅包含提供信息的少数文件。

_placeList = await DataModel.PlaceDataSource.GetData(url); _placeList =等待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. PlacePage.xaml.cs文件中的这一行语句实际上是在获取记录,但不会被绑定并显示在listview中。

But gridViewPlaces.ItemsSource = await DataModel.PlaceDataSource.GetData(url); 但是gridViewPlaces.ItemsSource =等待DataModel.PlaceDataSource.GetData(url); works. 作品。

You can find the source code here. 您可以在此处找到源代码。 Project Download Link 项目下载链接

MainPage.xaml 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 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 PagePage.xaml.cs文件

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());
}

Your PlaceList property needs to fire notifications to let the binding know there's a change. 您的PlaceList属性需要触发通知,以使绑定知道发生了更改。 As is, when you replace _placeList you don't notify anybody that PlaceList changed and so nothing updates. 照原样,当您替换_placeList时,您不会通知任何人PlaceList发生了更改,因此没有任何更新。 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. 这里的典型模式是以只读方式初始化PlaceList属性,然后将其添加到该现有集合中,而不是换出该集合,尽管如果您通知您已经交换了该集合也可以使用。

Additionally, the IEnumerable inside PlaceList needs to provide notifications when its contents change. 此外,PlaceList内部的IEnumerable内容更改时需要提供通知。 The standard way to do this is to make it an ObservableCollection since OC implements INotifyPropertyChanged and INotifyCollectionChanged for you. 执行此操作的标准方法是将其设置为ObservableCollection,因为OC为您实现了INotifyPropertyChanged和INotifyCollectionChanged。 See the Binding to collections Quickstart 请参阅绑定到收藏夹快速入门

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM