Search code examples
c#wpflistview

Hide listview header programmatically


I have a ListView like:

Col1  Col2  Col3
1      A     I
2      B     II
3      C     III 

I use 2 buttons. When I click on the first button the Col3 should collapse and it should be visible when a click in the second button.

Any idea on how to do such a ListView in WPF?


Solution

  • Use of Thumb will solve the problem. Just as

    <ListView x:Name="MyListView"IsSynchronizedWithCurrentItem="True" 
        ItemsSource="{Binding Path=Items}",  Mode=Default, 
        Source={StaticResource DataProvider}}"  
        Thumb.DragDelta="Thumb_DragDelta">
    
    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;
       }
    }