Search code examples
c#wpftreelistview

Where to find TreeListViews ColumnHeaderClick Event?


I'm developing a tool which shows data from a database in a hierarchical manner. As there are additional data for each item I'm using a TreeListView control to display them in additional columns. The number of columns is determined by user input.

The custom control that I'm using is Ricciolos TreeListView: http://windowsclient.net/blogs/ricciolocristian/archive/2008/03/22/a-complete-wpf-treelistview-control.aspx

My problem now is, that I need to catch the ColumnHeaderClick event to apply a sorting logic. I already interviewed auntie Google, but no results. Maybe somene here knows where to find such an event and how to determine which column header has been clicked.

Thanks


Solution

  • You would need to add a handler for the GridViewColumnHeader.Click event. This post describes how to do it for the ListView, which uses the same underlying controls. This code was adapted from that link:

    myTreeListView.AddHandler(GridViewColumnHeader.ClickEvent, new RoutedEventHandler(this.OnGridViewColumnHeaderClicked));
    
    private void OnGridViewColumnHeaderClicked(object sender, RoutedEventArgs e) {
        MessageBox.Show("testing");
    }
    

    Alternatively, you can attach a handler via XAML like so:

    <my:TreeListView GridViewColumnHeader.Click="OnGridViewColumnHeaderClicked" />
    

    The e.OriginalSource will include the GridViewColumnHeader, and e.Source/sender would be the TreeListView.