簡體   English   中英

無法綁定到Windows 10應用程序(UWP)中的列表視圖

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

我創建了示例應用程序來演示該問題。 抱歉,將所有代碼放在這里非常困難,因為這里有模型類,數據模型和服務文件,這些文件從rest api獲取數據。

因此,僅包含提供信息的少數文件。

_placeList =等待DataModel.PlaceDataSource.GetData(url); PlacePage.xaml.cs文件中的這一行語句實際上是在獲取記錄,但不會被綁定並顯示在listview中。

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

您可以在此處找到源代碼。 項目下載鏈接

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文件

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

您的PlaceList屬性需要觸發通知,以使綁定知道發生了更改。 照原樣,當您替換_placeList時,您不會通知任何人PlaceList發生了更改,因此沒有任何更新。 這里的典型模式是以只讀方式初始化PlaceList屬性,然后將其添加到該現有集合中,而不是換出該集合,盡管如果您通知您已經交換了該集合也可以使用。

此外,PlaceList內部的IEnumerable內容更改時需要提供通知。 執行此操作的標准方法是將其設置為ObservableCollection,因為OC為您實現了INotifyPropertyChanged和INotifyCollectionChanged。 請參閱綁定到收藏夾快速入門

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM