如何使控件填充 ListView 中的整个单元格(使用 GridView)?我玩过各种属性,但控件始终是最小尺寸。
这是我的xml。
<Window x:Class="ListViewUserControlTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ListViewUserControlTest"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<DataTemplate x:Key="UserControlCell">
<Rectangle Fill="Red" HorizontalAlignment="Stretch" MinWidth="10" MinHeight="10" />
</DataTemplate>
</Window.Resources>
<Grid>
<ListView Name="MyListView">
<ListView.View>
<GridView>
<GridViewColumn Width="30" Header="Col1" DisplayMemberBinding="{Binding Name}" />
<GridViewColumn Width="200" Header="Col2" CellTemplate="{StaticResource UserControlCell}" />
</GridView>
</ListView.View>
</ListView>
</Grid>
编辑 抱歉,我更改了问题,因为我已经消除了用户控件作为问题的根源。任何控制都会发生这种情况。
最佳答案
在缩小我的问题范围后,我正要谷歌并找到一个 answer here .
基本上由于某种原因,ListViewItems 设置为左对齐。将它们设置为 Stretch 可解决此问题。这是通过如下样式完成的:
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
不幸的是,这会影响每一列,但是您可以按照链接中的说明将其他列的内容设置为左、右、中心。
关于.net - 在 WPF 中拉伸(stretch)控件以填充 ListView 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/493345/