2012-02-07 70 views
0

为什么这个显示数据:列表框不显示数据

<ItemsControl ItemsSource="{Binding Path=.}"> 
       <ItemsControl.ItemTemplate> 
        <DataTemplate> 
         <StackPanel Orientation="Vertical"> 
          <TextBlock FontWeight="Bold" Text="{Binding Category}" /> 
          <TextBlock Text=", " /> 
          <TextBlock Text="{Binding Title}" /> 
          <TextBlock Text=" " /> 
          <Label Content="{Binding ImageUrl}" Foreground="Blue" /> 
         </StackPanel> 
        </DataTemplate> 
       </ItemsControl.ItemTemplate> 
      </ItemsControl> 

和下面显示空行(但一样多dataitems):

<ListBox ItemsSource="{Binding Path=.}"> 
       <ListBox.ItemTemplate> 
        <DataTemplate> 
         <StackPanel Orientation="Vertical"> 
          <TextBlock FontWeight="Bold" Text="{Binding Category}" /> 
          <TextBlock Text=", " /> 
          <TextBlock Text="{Binding Title}" /> 
          <TextBlock Text=" " /> 
          <Label Content="{Binding ImageUrl}" Foreground="Blue" /> 
         </StackPanel> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 
      </ListBox> 
+0

是否需要显式设置'DataTemplate DataType'? – 2012-02-07 17:05:30

回答

1
  1. 尝试把一个断点在你的getter类别。它受到打击吗?
  2. 看看VS中的输出窗口,你看到任何绑定错误?

我在Blend中用下面的代码创建了一个测试应用程序,我在这两种情况下都看到了列表。所以也许在你的其他代码(Binding,Code-behind,Viewmodel等)中存在一些问题,但是如果连接正确,你的ListBox和ItemsControl都应该可以工作。

<Grid x:Name="LayoutRoot" DataContext="{Binding Source={StaticResource SampleDataSource}, Path=Collection}"> 
    <Grid.RowDefinitions> 
     <RowDefinition></RowDefinition> 
     <RowDefinition></RowDefinition> 
    </Grid.RowDefinitions> 

    <ItemsControl Grid.Row="0" ItemsSource="{Binding Path=.}"> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Vertical"> 
         <TextBlock FontWeight="Bold" Text="{Binding Category}" /> 
         <TextBlock Text=", " /> 
         <TextBlock Text="{Binding Title}" /> 
         <TextBlock Text=" " /> 
         <Label Content="{Binding ImageUrl}" Foreground="Blue" /> 
        </StackPanel> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 

     <ListBox Grid.Row="1" ItemsSource="{Binding Path=.}"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Vertical"> 
         <TextBlock FontWeight="Bold" Text="{Binding Category}" /> 
         <TextBlock Text=", " /> 
         <TextBlock Text="{Binding Title}" /> 
         <TextBlock Text=" " /> 
         <Label Content="{Binding ImageUrl}" Foreground="Blue" /> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
</Grid>