tags:

views:

654

answers:

2

I have a databound list view and can set all the column widths manually except for the last. The data length of the last field varies greatly. The last column needs to expand to handle the width of the data and if greater scroll. I have tried setting the width of the last column to some very large number, but that drives the column header off the page. How do I calculate the min width that the last column needs to be to display the data and if it is large to keep the column header on the page?

<ListView x:Name="ListViewFlightPlans" Background="#FF000000" IsSynchronizedWithCurrentItem="True" Width="659.5" Height="Auto" 
                      Foreground="#FFFDF3F3"  BorderBrush="{x:Null}" VerticalAlignment="Bottom" Visibility="Hidden" >
                <ListView.View>
                    <GridView>
                        <GridViewColumn Header="CID" DisplayMemberBinding="{Binding Path=CID}" Width="50"/>
                        <GridViewColumn Header="Callsign" DisplayMemberBinding="{Binding Path=ACID}" Width="65"/>
                        <GridViewColumn Header="Type" DisplayMemberBinding="{Binding Path=Type}" Width="65"/>
                        <GridViewColumn Header="Route" DisplayMemberBinding="{Binding Path=Route}" Width="900 e"/>
                    </GridView>
                </ListView.View>
            </ListView>
A: 

You could try using % instead of pixels and perhaps allocate 60% to the last column whilest the remaining 40% could be used by the first 3 columns....(untested guess).

Jobo
+1  A: 

Because your header goes off the page, I assume that your headers are center- or right- aligned. Left-align the header to make it stay put (you can add padding on the left to fake a centered or right-aligned header).

Use Width="Auto" to make the width of the column expand based on the widest item in the column.

Example:

<GridViewColumn Header="Route" DisplayMemberBinding="{Binding Path=Route}" 
     Width="Auto"
     HeaderContainerStyle="{StaticResource WideColumnHeader}"
/>
...
‹!--Somewhere in your resources--›
...
‹Style x:Key="WideColumnHeader" TargetType="{x:Type GridViewColumnHeader}"›
   ‹Setter Property="HorizontalContentAlignment" Value="Left" /›
   ‹Setter Property="Padding" Value="40,0,0,0"
‹/Style›
...
Ria