3

How can I specify MinWidth for a Listview column in WPF?

Paolo Moretti
  • 54,162
  • 23
  • 101
  • 92

1 Answers1

3

This code uses the Thumb control. It will prevent the user to drag the header only in a specified width.

Add this in your WPF

<ListView x:Name="MyListView"
          IsSynchronizedWithCurrentItem="True" 
          ItemsSource="{Binding Path=Items,  
                                Mode=Default, 
                                Source={StaticResource DataProvider}}"
          Thumb.DragDelta="Thumb_DragDelta">

And in C# do as

public Window1() 
{  
  InitializeComponent();  
  MyListView.AddHandler(Thumb.DragDeltaEvent,new DragDeltaEventHandler(Thumb_DragDelta),true);
}
void Thumb_DragDelta(object sender, DragDeltaEventArgs e)
{ 
  Thumb senderAsThumb = e.OriginalSource as Thumb;  
  GridViewColumnHeader header = senderAsThumb.TemplatedParent as GridViewColumnHeader; 
  if (header.Column.ActualWidth < MIN_WIDTH)     
    header.Column.Width = MIN_WIDTH;   
  if (header.Column.ActualWidth > MAX_WIDTH)
    header.Column.Width = MAX_WIDTH;
}
Anders
  • 1,590
  • 1
  • 20
  • 37
Sauron
  • 16,668
  • 41
  • 122
  • 174