使WPF ComboBoxes填充整列宽度 [英] Make WPF ComboBoxes fill a whole column width
问题描述
我有一个问题使 ComboBox
拉伸到 GridViewColumn
中填充整个列宽。当列调整大小时,它也应该调整大小。
I'm having problems making a ComboBox
stretch to fill the whole column width in a GridViewColumn
. It should also resize when the column is resized.
在下面的示例中,我有一个 StackPanel
和 ComboBox
里面。这被设置为拉伸,并且实际上拉伸以填充 StackPanel
宽度。
In the following example I have a StackPanel
with a ComboBox
inside. This is set to stretch and will in fact stretch to fill the StackPanel
width.
然后,我在一列中添加一个 ListView
,其中包含 StackPanel
与 ComboBox
。 StackPanel
和 ComboBox
都设置为stretch,但它们不会。我使用背景颜色来标识 StackPanel
的大小,除非我设置一个宽度或添加元素到 ComboBox
这样它需要更多的宽度。
Then I add a ListView
with one column, containing a StackPanel
with a ComboBox
. Both the StackPanel
and the ComboBox
are set to stretch, but they don't. I use background colors to identify the size of the StackPanel
s, and there is no red unless I set a width or add elements to the ComboBox
such that it needs more width.
我也尝试过使用 HorizontalContentAlignment
属性,但没有成功。
I also tried playing around with the HorizontalContentAlignment
property without success.
<StackPanel Height="59" Margin="45,12,38,0" VerticalAlignment="Top" Background="Green">
<ComboBox HorizontalAlignment="Stretch" />
</StackPanel>
<ListView x:Name="MyListView" Margin="0,106,0,0">
<ListView.View>
<GridView>
<GridViewColumn Header="Num" Width="70">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel Background="red" Orientation="Horizontal" HorizontalAlignment="Stretch">
<ComboBox HorizontalAlignment="Stretch" />
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
<ListViewItem></ListViewItem>
</ListView>
推荐答案
尝试设置 / code>的
ListViewItem
。我也删除了您的StackPanel。
Try setting the Style
of the ListViewItem
. I also removed your StackPanel.
<ListView x:Name="MyListView" Margin="0,106,0,0">
<ListView.Resources>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListView.Resources>
<ListView.View>
<GridView>
<GridViewColumn Header="Num" Width="170">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ComboBox />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
<ListViewItem></ListViewItem>
</ListView>
这篇关于使WPF ComboBoxes填充整列宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!