silverlight listbox 无法 stretch

如果将listbox的ItemTemplate绑定到DataTemplate,会发现listbox无法stretch。

解决办法是将listbox的ItemContainerStyle绑定到一个style。如下所示:

<Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Top"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<StackPanel Orientation="Vertical" HorizontalAlignment="Stretch" Margin="0 0 0 25">
<Grid Background="#ebf8ff" HorizontalAlignment="Stretch" Height="25">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding CreateDate,Mode=TwoWay}" Foreground="#686868" VerticalAlignment="Center" Margin="12 0 0 0"/>
<TextBlock Text="{Binding CreateBy,Mode=TwoWay}" Foreground="#0b4292" Margin="10 0" VerticalAlignment="Center"/>
<TextBlock Text="{Binding FollowReportTitle,Mode=TwoWay}" VerticalAlignment="Center"/>
</StackPanel>
</Grid>
<TextBlock Text="{Binding Body,Mode=TwoWay}" HorizontalAlignment="Stretch" TextWrapping="Wrap" Margin="12 5 0 5" />
<ItemsControl x:Name="icAttachments" ItemsSource="{Binding Attachments,Mode=TwoWay}"
VerticalAlignment="Center" ItemTemplate="{StaticResource listBoxFollowStyle}" Margin="12 0 0 0">
<ItemsControl.Template>
<ControlTemplate TargetType="ItemsControl">
<ItemsPresenter />
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Name="sp" Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>



posted @ 2012-02-13 15:27  左手边的爱  阅读(347)  评论(0编辑  收藏  举报