views:

664

answers:

1

How to properly handle listview's last column sizing in such a way that:

  1. listview does not display the ugly and unused 'extra' column (see picture 1, or here for example)
  2. the (actual) last column is stretched to fill the remaining space
  3. the horizontal scrollbar is not shown
  4. resizing in any direction leaves no artefacts (see below)

I've been struggling with this for quite some time and it doesn't work out, nor did numerous searches on the web provide any insight. Here's what I have so far:

  1. I first tried adjusting the width of the last column to exactly fill the entire view, but then the horizontal scrollbar pops up. So now I set all column's ColumnHeaderContainerStyle to a transparent style that doesn't show anything, and override the HeaderContainerStyle on each Column with the actual style; for the last column, I made the margin negative so that it fills the extra empty space that originates from the extra column. See picture 2

  2. calculate last column width as difference between actualWidth and sum of widths of other columns taking into account the margin/padding

  3. same as for 2; width cannot be too small or else the horizontal scrollbar is shown

  4. this is introduced by 2: if the last column's width is set to a constant value, I can grow and shrink without problems. When using the code to stretch the column, the view properly stretches when growing, but when shrinking a part of the listview's rows disappear. The column header however stays more or less properly sized, but is still smaller then normal, see picture 3. This makes me think there's something special about shrinking: the calculation I do is the same, yet visually it's not. However I could not find where/how this happens in the framework.

Pictures:

Picture 1 Picture 2 Picture 3

Code for stretching, applied to last column's with in ListView resize handler

int StretchLastColWidth( ListView lv, int minWidth )
{
  double width = lv.ActualWidth;
  if( width <= 0 )
    return minWidth;

  GridView grdView = lv.View as GridView;
  for( int i = 0 ; i < grdView.Columns.Count - 1 ; ++i )
    width -= ( grdView.Columns[ i ].ActualWidth + 6 ); //padding?

  Decorator border = VisualTreeHelper.GetChild( lv, 0 ) as Decorator;
  ScrollViewer scroll = border.Child as ScrollViewer;
  if( scroll.ComputedVerticalScrollBarVisibility == System.Windows.Visibility.Visible )
    width -= 14; //my scrollbar is smaller than the system default..

  width -= 12  //comes from padding??

  if( width > minWidth )
    return Convert.ToInt32( width );
  else
    return minWidth;
}

Listview's xaml:

<ListView ItemContainerStyle="{StaticResource ListViewItemStyle}">
  <ListView.View>
    <GridView ColumnHeaderContainerStyle="{DynamicResource ListViewEmptyHeaderStyle}">
      <GridViewColumn Width="80" Header="Hdr0" HeaderContainerStyle="{DynamicResource ListViewHeaderStyle}"/>
      <GridViewColumn Header="Hdr1" HeaderContainerStyle="{DynamicResource ListViewLastHeaderStyle}"/>
    </GridView>
  </ListView.View>
</ListView>

If really needed I could post the entire template; note that although I've shown the pictures using a rather heavily my modified style, the results are exactly the same when using a standard ListView without modification. Should anyone want to try, use this style to see the border around the row the items:

<Style x:Key="ListViewItemStyle" TargetType ="{x:Type ListViewItem}">
  <Setter Property="BorderBrush" Value="White"/>
  <Setter Property="BorderThickness" Value="1"/>
  <Setter Property="Margin" Value="3,1"/> <!--if not used, border sticks to scrollbar etc-->
  <Setter Property="SnapsToDevicePixels" Value="true"/>
</Style>
A: 

Check out this CodeProject article:

ListView Layout Manager By Jani Giannoudis

Aviad P.
I already checked it out some time ago; while it is a great article, the author chose to ignore the fact that he's having the exact same problem as I posted: at first, his row borders *always* look as the one in my 3rd picture. Second, if I adjust his code to account for that (add a simple 'width -= 10' or so), the border is properly shown, but from the moment I shrink the view it's right edge disappears, exactly as my code does.
stijn
I think the only real solution to this problem is to write a GridViewHeaderRowPresenter and GridViewRowPresenter from scratch, or use some kind of IL disassembler to copy and modify the framework ones...
Aviad P.
you're probably right; thankfully the software I use this ListViews in is not true production software so maybe by he time I'll dig into it someone found a proper soluion already.
stijn