带有Wrappanel的列表框不会显示元素

时间:2011-05-28 06:34:24

标签: silverlight silverlight-4.0 silverlight-toolkit wrappanel

当我为我的Silverlight 4 ListBox ItemsPanel插入WrapPanel时,我感到非常困惑,没有任何显示。如果我只注释掉ItemsPanel,我会得到一张正常的带有文字的小图片的垂直列表。我将背景颜色添加到WrapPanel只是为了说服自己WrapPanel实际上在那里。我想我错过了一些愚蠢的东西,它是什么?

    <ListBox Grid.Row="1" Grid.Column="1"
             ScrollViewer.HorizontalScrollBarVisibility="Disabled"
             MinHeight="24"
             >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Border> <StackPanel> <Image> <TextBlock> </StackPanel> </Border> (pseudo template)
            </DataTemplate>
        </ListBox.ItemTemplate>
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <toolkit:WrapPanel Background="Orange" />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
    </ListBox>

3 个答案:

答案 0 :(得分:0)

项目模板中没有任何内容可以包含任何内容 - 它充满了空元素。把东西放在那里,你应该看到结果。 e.g:

<DataTemplate>
    <TextBlock>Test</TextBlock>
</DataTemplate>

答案 1 :(得分:0)

我使用Blend的示例数据重现了您的场景,我可以看到包装面板中的项目:

    <UserControl
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
        x:Class="ASD_Answer012.MainPage"
        Width="640" Height="480">
        <UserControl.Resources>
            <DataTemplate x:Key="ItemTemplate">
                <StackPanel>
                    <TextBlock Text="{Binding Property1}"/>
                    <CheckBox IsChecked="{Binding Property2, Mode=TwoWay}"/>
                    <Image Source="{Binding Property3}" HorizontalAlignment="Left" Height="64" Width="64"/>
                </StackPanel>
            </DataTemplate>
            <ItemsPanelTemplate x:Key="ItemsWrapPanelTemplate">
                    <toolkit:WrapPanel Background="DarkOrange"/>
            </ItemsPanelTemplate>
        </UserControl.Resources>

        <Grid x:Name="LayoutRoot" Background="White" DataContext="{Binding Source={StaticResource SampleDataSource}}">
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <ListBox Grid.Column="1" Grid.Row="1" ItemTemplate="{StaticResource ItemTemplate}" ItemsSource="{Binding Collection}" ItemsPanel="{StaticResource ItemsWrapPanelTemplate}" ScrollViewer.HorizontalScrollBarVisibility="Disabled"/>
        </Grid>
    </UserControl>

这就是我得到的: WrapPanel Items

也许如果你提供更详细的XAML,就可以重现确切的问题并解决它。

答案 2 :(得分:0)

我对于为什么一无所知,但切换到我的ItemsSource的ObservableCollection就行了。不知道为什么默认的ItemsPanel的行为与diff't panel不同,但确实如此。

感谢您对此进行调查。