ListBox不显示数据

时间:2012-02-07 15:36:39

标签: wpf

为什么显示数据:

 <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>

1 个答案:

答案 0 :(得分:1)

  1. 尝试在类别的getter中添加断点。它会被击中吗?
  2. 查看VS中的“输出”窗口,您是否看到任何绑定错误?
  3. 我在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>