Listbox with Wrappanel won't show elements
I'm quite perplexed as to why when I drop in a WrapPanel for my Silverlight 4 ListBox ItemsPanel, nothing shows up. If I only comment out the ItemsPanel, I get a normal vertical list of my little pictures with text. I added the background color to the WrapPanel just to convince myself the WrapPanel was actually there. I assume I'm missing something boneheaded, what is it?
<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" />
</开发者_StackOverflow中文版ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
I have reproduced your scenario using Blend's sample data and I can see the items inside the wrap panel:
<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>
This is what I get:
Maybe if you provide more detailed XAML it will be possible to reproduce the exact issue and solve it.
I've clueless as to why, but switching to an ObservableCollection for my ItemsSource did the trick. No clue why things would behave differently for the default ItemsPanel than a diff't panel, but it did.
Thanks for looking into this.
Nothing in your item template has any substance to it - it's full of empty elements. Put something in there and you should see results. e.g.:
<DataTemplate>
<TextBlock>Test</TextBlock>
</DataTemplate>
精彩评论