问题背景
WPF ListBox 重写了 ListBox.ItemTemplate 样式后,ItemTemplate内的控件一直不能满铺。
问题定位
根据VS 的WPF 树结构调试器,发现ListBoxItem 高度是正确的,但是 ContentPresenter 的高度始终不能和父级(ListBoxItem)一样高。
解决办法
设置ListBoxItem 的 VerticalContentAlignment
<ListBox x:Name="course_list" ItemsSource="{Binding CoursesDatas}" VerticalAlignment="Stretch"
Grid.Row="1" BorderThickness="0" SizeChanged="ListBox_SizeChanged" >
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Padding" Value="0"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="MinHeight" Value="{Binding Path=DataContext.SectionHeight,RelativeSource={RelativeSource Mode=FindAncestor,AncestorLevel=1, AncestorType={x:Type UserControl}}}"/>
//设置ListBoxItem里面的内容控件满铺
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
</Style>
</ListBox.ItemContainerStyle>
...
...
效果如下图:
样式有问题
修改样式后