0

我很困惑为什么当我为我的 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>
4

3 回答 3

0

您的项目模板中的任何内容都没有任何实质内容 - 它充满了空元素。把东西放在那里,你应该会看到结果。例如:

<DataTemplate>
    <TextBlock>Test</TextBlock>
</DataTemplate>
于 2011-05-28T15:57:17.583 回答
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 项目

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

于 2011-05-29T16:27:50.370 回答
0

我不知道为什么,但是为我的 ItemsSource 切换到 ObservableCollection 就成功了。不知道为什么默认 ItemsPanel 的行为与 diff't 面板不同,但确实如此。

感谢您查看这个。

于 2011-05-29T21:29:25.437 回答