0

I am using a ListView in WPF. For extending the last column width I am using an approach similar to this:

How to resize a certain control based on Window size in WPF?

When I implement the attached code, always a horizontal scrollbar appears, so I understand that the content of the ListView is larger then the control. It´s possible to hide the visibility of the scrollbar, but it doesn't´t solve the problem.

public class WidthConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {

        double listViewWidth;
        double otherColumnsTotalWidth = 0;
        double.TryParse(values[0].ToString(), out listViewWidth);
        var arrayOfColumns = values[1] as IList<GridViewColumn>;

        for (int i = 0; i < arrayOfColumns.Count - 1; i++)
        {
            otherColumnsTotalWidth += arrayOfColumns[i].Width;
        }



        return (listViewWidth - otherColumnsTotalWidth) < 0 ? 0 : (listViewWidth - otherColumnsTotalWidth);

    }

        public object[] ConvertBack(object value, Type[] targetTypes,
                                    object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

and XAML:

<GridViewColumn Header="Text" DisplayMemberBinding="{Binding Text1}">
                    <GridViewColumn.Width>
                        <MultiBinding Converter="{StaticResource LastColumnWidtConverter}">
                            <Binding Path="ActualWidth" RelativeSource="{RelativeSource AncestorType=ListView}"/>
                            <Binding Path="View.Columns" RelativeSource="{RelativeSource AncestorType=ListView}"/>
                        </MultiBinding>
                    </GridViewColumn.Width>

  1. Why does the HorizontalScrollBar appear, even when the content is smaller? Here basically the width of the columns is count and subtracted from the entire width. So the remaining width should fit perfectly to fill the remaining space.

  2. Looking at the last column (which should be extended) on the right side a separator appears in the column header. Why is this happening?

enter image description here

How can I handle this?

Community
  • 1
  • 1
Robby
  • 23
  • 8
  • Let me answer your 2nd question first: The last column does not fill all of the available space (its width being slightly too small). So what you see on the right side of the grid view is just the remaining space not being covered by the last column. What you call "separator" is simple the right edge of the last column... –  May 16 '17 at 19:22
  • Now to your (1) question: Your converter has one problem - it takes the `Width` of each column instead of the `ActualWidth`. This would only work if all the columns (except the last column of course) have a width set explicitely. As soon as you have auto-size columns, the converter falls flat on its face due to this problem. The fix is simple: use `ActualWidth` instead of `Width`. (contd...) –  May 16 '17 at 19:24
  • Even if you change the converter to use ActualWidth of the columns, probably the width of the last column is still not correct (as you would need to take into account any possible padding, border and such of the scrollviewer and/or item panels). And even then there is yet another problem - if the user is going to resize one of the columns, your binding will not be updated (as the binding does not bind directly to each columns ActualWidth property). So it gets complicated fast... (contd.) –  May 16 '17 at 19:46
  • What i would suggest is to use some behavior for exactly the effect you are after. Luckily, someone already made such behavior :) [Look at this answer](http://stackoverflow.com/a/6529374/2819245) to a similar question. I haven't tried this behavior by myself, so i can't tell how well it works. But it should be worth a try, i guess... Oh, and as a side note; you might want to disable the horizontal scrollbar for your ListView, just to be sure... (I am finished now (: ) –  May 16 '17 at 19:53

0 Answers0