views:

2467

answers:

2

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>
A: 

Unless the StackPanel is going to have something in it, you may have better luck with a Border.

Jeff Wain
Aware that the StackPanel can be omitted. The example is a simplification of my real problem - where I seem to need a surrounding StackPanel. Therefore I kept it for the question. Please see my comment to @bendeway's answer for details.
stiank81
+5  A: 

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>
bendewey
Thanks! That works, however - it gives me a new problem. Well, in the simplified example I gave in the question it is all good, but my real list is more complex. When adding the styling - without having any StackPanel around - resizing the column is really slow. If I add a surrounding StackPanel with the property Orientation="Horizontal" resizing is smooth. This problem occurs when I have typically >1000 elements in my list. Any idea what gives this? Not using your solution, but adding Background="Transparent" gives the same issue. Feels like I've tried everything.. Thanks!
stiank81
Might add that adding a surrounding StackPanel with horizontal orientation stops the ComboBox from filling the whole column...
stiank81
Well, you did answer the question, so I'm closing this one. Thx. Please check out my new question about the column resize performance issues: http://stackoverflow.com/questions/829242/wpf-column-resize-performance-issues
stiank81