为什么我的类拒绝绑定到ListBox?

时间:2012-11-24 11:30:09

标签: c# windows-phone-7

我不知道我的代码有什么问题,它没有显示任何错误,但是当我运行代码时,模拟器中没有列表框出现。代码的目的是在旁边显示一个缩略图。 超链接元素。我已经检查过所有图像位置都是有效的,并且所有图像都已设置为类型内容。

 public class element
    {
       public string imageLocation { get; set; }
       public string name {get; set; }
    }

    var source = List<element>();
    //I then populate source with 4 elements (code omitted)
    //The source list was created successfully

    listBoxName.itemsSource = source; 

在Xaml

<ListBox Name ="listBoxName"
                     HorizontalAlignment="Left"
                     VerticalAlignment="Top"
                                            >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Canvas Name="List" 
                                 Tap="tapped_Click" 
                                 Background="Blue"   
                                 Height="100" Margin="0,0,0,0">

                            <Image  Name="Thumbnail"    
                                    Source="{Binding imageLocation}"     
                                     Height="102" Width="126" />

                            <HyperlinkButton Name="link" 
                                             Content="{Binding name}" 
                                             Margin="0,0,0,84" Canvas.Left="128" 
                                             Canvas.Top="2" Height="96" Width="348" 
                                             FontSize="30" HorizontalAlignment="Left"/>
                        </Canvas>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

1 个答案:

答案 0 :(得分:0)

您的列表框已正确绑定,但由于其宽度等于0而未显示。

设置画布的宽度,或使用其他类型的容器(如网格):

<ListBox Name ="listBoxName"
                HorizontalAlignment="Left"
                VerticalAlignment="Top">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Canvas Name="List" 
                    Tap="tapped_Click" 
                    Background="Blue"   
                    Height="100" Width="476" Margin="0,0,0,0">

                <Image  Name="Thumbnail"    
                        Source="{Binding imageLocation}"     
                        Height="102" Width="126" />

                <HyperlinkButton Name="link" 
                                Content="{Binding name}" 
                                Margin="0,0,0,84" Canvas.Left="128" 
                                Canvas.Top="2" Height="96" Width="348" 
                                FontSize="30" HorizontalAlignment="Left"/>
            </Canvas>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>