3

How can I get the headerclick event of a WPF Listview?

3
  • 3
    Seriously! Why couldn't you Google or look at MSDN for this information?! Commented May 22, 2009 at 4:19
  • 1
    Yeah hard to disagree, Dennis. I guess because it's "GridViewColumnHeader" it might be a bit hard to search for, but it probably would've been easier than typing in the question and waiting for an answer. Commented May 22, 2009 at 4:28
  • 1
    If you're pretty new to WPF, it isn't that obvious that the event exists. It's neither in the Properties pane nor in IntelliSense in Visual Studio 2010. Commented Sep 18, 2014 at 10:53

2 Answers 2

11

You can use the GridViewColumnHeader.Click attached event. As an example, see the MSDN page on sorting a GridView when the header is clicked.

<ListView x:Name='lv' 
          Height="150" 
          HorizontalAlignment="Center" 
          VerticalAlignment="Center" 
          GridViewColumnHeader.Click="GridViewColumnHeaderClickedHandler">
Sign up to request clarification or add additional context in comments.

3 Comments

Events that apply to children like that won't show up in Intellisense - the editor can't know which ones you might want to use!
worked perfectly and this is how i got the bound column, \string columnProperty = ((Binding)columnHeader.Column.DisplayMemberBinding).Path.Path;
2

just to expand on the previous answer, how to know which header was clicked on:

XAML:

<ListView GridViewColumnHeader.Click="GridViewColumnHeaderClickedHandler">

CS:

private void SortHeaderClick(object sender, RoutedEventArgs e)
{
    MessageBox.Show(((GridViewColumnHeader)e.OriginalSource).Column.Header.ToString());
}

1 Comment

If somebody is searching for the possibility to sort ListView items by ordered LINQ query after acquaring the WPF ListView EventHandler, then there is a compact solution: var query = dataList.OrderByWithDirection(x => x.Property, direction); <stackoverflow.com/questions/388708/…>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.