tags:

views:

453

answers:

1

I am trying to resize a ListView in WPF. I want to figure out how to force a ListView to shrink it's width to only fit the width of it's columns. I have a ListView that I bind using the View property and add some GridViewColumns like so:

                <ListView ItemsSource="{Binding MyCollection}">
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Header="Name" Width={Binding NameWidth} DisplayMemberBinding="{Binding Name}"/>
                            <GridViewColumn Header="Fee" DisplayMemberBinding="{Binding Fee}"/>

                        </GridView>
                    </ListView.View>
                </ListView>

I bind the width of the column so that I can hide/unhide the column. When I unhide a column the ListView grows to accommodate the new width. But when I hide the column again the ListView's width doesn't shrink.

I have tried to subclass the ListView and call some commands like

InvalidateArrange(), InvalidateMeasure(), InvalidateVisual(). But none of them cause the ListView to shrink.

If I reload the Collection to the ListView it does shrink to fit. But I think that is not elegant, nor the right way to do it. I have journeyed through Reflector to find myself in a quagmire of confusion. Can someone give me some insight?

A: 

Your ListView is likely being sized by its parent container, not by its contents. For example, a Grid, StackPanel, DockPanel, Border, etc., outside your ListView may be stretching your ListView to fill its width.

If this does not solvethe problem, please test your ListView in an empty Window with a root WrapPanel to factor out any other surrounding XAML in your control and post the entire XAML for that Window.

Jerry Bullard