wpf - HorizontalAlignment or VerticalAlignment don't work as expected
问题描述
Everytime I think I begin to understand how WPF is working, it's doing something (or doesn't do it) and I don't understand why. May you can shed a light.
I'd like to place some numbers on the screen, the numbers should be surrounded by a Border
and the complete Border
should have a colored background (not just the bounding box of the TextBlock
containing the numbers.)
So I did something like this:
<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>
But although I declared HorizontalAlignment
and VerticalAlignment
as Stretch
the border (and a background I didn't include in the example) only cover the bounding box of the TextBlock
, not the complete cell of the UniformGrid
.
Am I missing something here?
解决方案
这是因为默认情况下列表框项目的内容展示器不会拉伸。因此,您必须将其添加到您的 ListBox:
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
</Style>
</ListBox.ItemContainerStyle>
推荐阅读
- r - 尝试将 2.5 gb csv 文件读入 R 时出错
- python - 用相同的颜色绘制每个集群
- informatica - 支持 Informatica 集成分配步骤中的可选参数
- excel - 配置模块
- html - 可以在 css 文件中设置 contenteditable 吗?
- swift - 如何使用 2 个 Swift 协议创建观察者模式,其中两个关联类型必须相同?
- java - logback-spring.xml 中的 LogstashTcpSocketAppender 不会将日志发送到 ELK 但适用于基于 Java 的方法
- oracle-apex - 交互式报表中的列标题
- excel - 如果命令 Application.run 不应该能够打开工作簿,为什么引用的工作簿打开两次?
- r - 在 x 轴线上方有 ggplot2 geom_points