Why does the horizontal Scrollbar appear in ListView when the content is smaller?

449 Views Asked by At

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?

0

There are 0 best solutions below