2017-10-28 87 views
0

我已經創建了用於演示此問題的示例應用程序。 對不起,它很難把所有的代碼放在這裏,因爲有模型類,datamodel,服務文件,從rest api提取數據。無法綁定到Windows 10應用程序中的列表視圖(UWP)

所以只有很少的文件被包含在信息中。

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

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

你可以在這裏找到源代碼。 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文件

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

回答

1

您的PlaceList屬性需要觸發通知以讓綁定知道有變化。因爲,當您替換_placeList時,您不會通知任何人PlaceList發生更改,因此沒有任何更新。此處的典型模式是將PlaceList屬性初始化爲只讀,然後將事物添加到現有集合中,而不是將集合交換出去,儘管如果您通知您已交換了也應該工作的集合。

此外,PlaceList中的IEnumerable需要在其內容更改時提供通知。執行此操作的標準方法是使其成爲ObservableCollection,因爲OC爲您實現INotifyPropertyChanged和INotifyCollectionChanged。查看Binding to collections快速入門

+0

是的,我試着改變ObservableCollection,但仍然無法正常工作。從提供的示例中,如果看到MainPage.xaml.cs文件使用了相同的概念,並在那裏使用splitview中的綁定列表視圖。 – Sharath

+0

這只是它的一半。您還需要通知您已更改收藏集。查看更新的文本 –

相關問題