Saturday, July 25, 2015

Minimum column width in WPF ListView/GridView

It seems that it is not possible to set the minimum width of a column within XAML. But it can be forced withn code-behind. Laurent Bugnion has found the solution (

The ListView is defined in the XAML part.
            <ListView x:Name="MyListView">
                <!-- ... -->

In the constructor of the code-behind part the event handler is added.
                new DragDeltaEventHandler(Thumb_DragDelta), true);

The event handler takes care that column width is not set under 20 (in this example).
        private void Thumb_DragDelta(object sender, DragDeltaEventArgs e)
            Thumb senderAsThumb = e.OriginalSource as Thumb;
            GridViewColumnHeader header = senderAsThumb.TemplatedParent as
            if (header == null)
            if (header.Column.ActualWidth < 20)
                header.Column.Width = 20;