Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make WPF ComboBoxes fill a whole column width

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.

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.

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 StackPanels, and there is no red unless I set a width or add elements to the ComboBox such that it needs more width.

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>
like image 768
stiank81 Avatar asked May 05 '09 21:05

stiank81


1 Answers

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>
like image 171
bendewey Avatar answered Sep 20 '22 13:09

bendewey