Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7939669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:10:21+00:00 2026-06-03T23:10:21+00:00

I am using attached behaviours to add the ability to sort a ListView by

  • 0

I am using attached behaviours to add the ability to sort a ListView by clicking on a column’s header. The behaviour adds the following handler to handle the user clicking on a GridViewColumnHeader:

listView.AddHandler(GridViewColumnHeader.ClickEvent, new RoutedEventHandler(ColumnHeader_Click));

The handler looks something like this:

static void ColumnHeader_Click(object sender, RoutedEventArgs e)
{
   var listView = sender as ListView;
   var header = e.OriginalSource as GridViewColumnHeader;
   var gridView = ((GridView)(listView.View));
   ...
}

I just noticed that if the ListView has a scroll bar, and I click on the scroll bar’s ‘shaft’ or scroll arrows (but not thumb!):

Anatomy of a Scroll Bar
(source: microsoft.com)

then the GridViewColumnHeader.ClickEvent is triggered, and my code fails because ‘header’ is now null. Obviously this isn’t an expected behaviour, and now I have to make sure that the OriginalSource is a GridViewColumnHeader.

Why does this happen?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:10:23+00:00Added an answer on June 3, 2026 at 11:10 pm

    The problem is, GridViewColumnHeader.ClickEvent actually is ButtonBase.ClickEvent, which means that the handler you set will be triggered by a user click on any button (or derived: checkbox, datepicker, scrollbar button, etc.) in the ListView, and not only on a column header (which is derived from button).

    See http://social.msdn.microsoft.com/Forums/en/wpf/thread/3595d153-4faa-4501-9c6f-2f074658e760

    The only solution I’ve found is to check that header != null before doing anything else, to exit the handler when the button that triggered it (e.OriginalSource) was not a column header.

    Hope this helps…

    (PS: also check if header.Column != null, to avoid an error when the user clicks on the “last extra header”, the empty header on the right of all the “regular” headers…)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using an attached Behaviours to add drag and drop functionality to my
I'm using the following code attached to a button, to attempt to open a
In Excel I have attached a picture to a cell using the following Sub
I am using a custom default button attached behaviour (as defined here: Silverlight 4
I am using following attached date picker. http://rapidshare.com/files/417385026/jQuery_Date_Picker.zip http://www.2shared.com/file/d2NK63P-/jQuery_Date_Picker.html Problem is if i don't
I'm trying to call a Page Method using a jQuery 'attached' event function, in
In my application I have to send a picture as the attached file using
I have attached an event to a text box using addEventListener . It works
I'm using a tutorial plugin - http://particlebits.com/code/jquery-tutorial/ which has this code attached to the
In Cocoa, a drawer can be attached to a window using the setParentWindow: method.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.