【发布时间】:2011-07-26 05:35:17
【问题描述】:
我正在构建一个需要有五列的 ListView - 第一个需要有可以是任意长度的文本,并且需要在窗口大小发生变化时换行(除了更改行高以使换行的文本可见) 和其他四列是 45 的静态宽度。我一直在寻找几个小时,我遇到的每个解决方案要么需要静态宽度,要么不起作用。
尝试过的解决方案:
auto、1*、2* 等的列宽(忽略设置) DockPanel(忽略设置) WrapPanel(忽略) 将宽度设置为实际宽度的父级的相对源(忽略)
有什么想法吗?似乎很多人都遇到过同样的问题,但我非常希望不必为这个专栏走静态宽度路线。特别是因为无论如何我这样做时内容都会被切断(即使该行使用 height="Auto" )。整个窗口的宽度可以小到 1024,但也可以是 1600+,这就是我想要动态调整大小的原因。这样一来,较小的屏幕将进行内容换行,而较大的屏幕将只显示一行,因为内容适合。
这是 XAML:
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition Width="45" />
<ColumnDefinition Width="45" />
<ColumnDefinition Width="45" />
<ColumnDefinition Width="45" />
</Grid.ColumnDefinitions>
<!-- This is the TextBlock that needs to wrap its content (and
change the height of the row (so the full content is still
visible) to whatever the available space is, but should not
make overall ListView wider than the parent's width. -->
<TextBlock Text="{Binding Content}" Padding="20,6,6,6" />
<!-- These four blocks will have other content eventually, but only need
to be 45 wide -->
<TextBlock Text="X" Grid.Column="1" HorizontalAlignment="Center" />
<TextBlock Text="X" Grid.Column="2" HorizontalAlignment="Center" />
<TextBlock Text="X" Grid.Column="3" HorizontalAlignment="Center" />
<TextBlock Text="X" Grid.Column="4" HorizontalAlignment="Center" />
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
【问题讨论】:
标签: wpf listview grid word-wrap