每次我认为我开始了解 WPF 的工作原理时,它正在做某事(或不做),但我不明白为什么。愿你能发光。
我想在屏幕上放置一些数字,数字应该被 a 包围,Border
并且完整的Border
应该有一个彩色背景(不仅仅是TextBlock
包含数字的边界框。)
所以我做了这样的事情:
<ListBox ItemsSource="{Binding Numbers}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="3" Columns="4" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Border HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderThickness="1" BorderBrush="Black">
<TextBlock Text="{Binding .}" />
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
但是,尽管我声明HorizontalAlignment
andVerticalAlignment
作为Stretch
边框(以及我没有在示例中包含的背景)仅覆盖 的边界框,而TextBlock
不是UniformGrid
. 我在这里错过了什么吗?
这是因为默认情况下列表框项的内容展示器不会拉伸。因此,您必须将此添加到您的列表框:
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
</Style>
</ListBox.ItemContainerStyle>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句