开发者

Panorama Binding on WP7, Items collection must be empty before using ItemsSource

i keep getting this error: Items collection must be empty before using ItemsSource, when i attempt to bind data to a Panorama control. below is my xaml snippet.

<controls:Panorama x:Name="panorama">
 <controls:PanoramaItem >
  <StackPanel>
   <TextBlock Text="{Binding Text}"/>
  </StackPanel>
 </controls:PanoramaIt开发者_开发百科em>
</controls:Panorama>

in my code behind (xaml.cs), i do something like this:

protected override void OnNavigatedTo(NavigationEventArgs e) {
 string id = NavigationContext.QueryString["id"];
 ObservableCollection<MyObject> list = DataAccessService.get(id);
 panorama.ItemsSource = list;
 base.OnNavigatedTo(e);
}

note that MyObject has a Text property. any help is appreciated.

after modifying it per the link below, the same exception is still thrown.

<controls:Panorama x:Name="panorama">
 <controls:Panorama.HeaderTemplate>
  <DataTemplate>
   <TextBlock Text="{Binding Header}"/>
  </DataTemplate>
 </controls:Panorama.HeaderTemplate>
 <controls:PanoramaItem >
  <DataTemplate>
   <StackPanel>
    <TextBlock Text="{Binding Text}"/>
   </StackPanel>
  </DataTemplate>
 </controls:PanoramaItem>
</controls:Panorama>

finally, after continuing further with the user below's help, this is the solution that got rid of the exception.

<controls:Panorama x:Name="panorama">
 <controls:Panorama.HeaderTemplate>
  <DataTemplate>
   <TextBlock Text="{Binding Header}"/>
  </DataTemplate>
 </controls:Panorama.HeaderTemplate>
 <controls:Panorama.ItemTemplate>
  <DataTemplate>
   <StackPanel>
    <TextBlock Text="{Binding Text}"/>
   </StackPanel>
  </DataTemplate>
 </controls:Panorama.ItemTemplate>
</controls:Panorama>


Your problem is that you're building the Panorama in XAML as though it were static rather than preparing it to be data-bound.

Take a look at this quick tutorial for a Data Binding Panorama Control:

Data Binding Panorama Control WP7 MVVM

Notice the difference in how your XAML is being constructed for the control. Instead of setting the Items collection on the Panorama control, you need to set the HeaderTemplate and ItemTemplate so that the control knows how to render things once data is bound to it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜