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?