为什么显示数据:
<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 :(得分:1)
我在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>