2012-02-07 67 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>