How do i captured event when user click on ultra tree column header.
thanks in advanced. any suggestion appreciated.
There is no event that specifically corresponds to the end user clicking on a column header, but there is an AfterSortChange event which fires after a column is sorted. If your objective is to receive a notification when a column is sorted, that is the event you should handle.
You can also handle the MouseDown event and hit test for an element of type UltraTreeNodeColumnHeaderUIElement, as demonstrated by the following code sample:
void ultraTree_MouseDown(object sender, MouseEventArgs e){ UltraTree tree = sender as UltraTree; UIElement controlElement = tree.UIElement; UIElement elementAtPoint = controlElement != null ? controlElement.ElementFromPoint( e.Location ) : null; UltraTreeNodeColumnHeaderUIElement headerElement = null;
while ( elementAtPoint != null ) { headerElement = elementAtPoint as UltraTreeNodeColumnHeaderUIElement; if ( headerElement != null ) break;
elementAtPoint = elementAtPoint.Parent; }
if ( headerElement != null ) { // If you get in here, the mouse was pressed over a column header. }}
Hi Brian,
I've got a very similar problem. I want to handle the Click event on the column header or either handle the SortEvent.
I've already handled the BeforeSortChange and AfterSortChange events, but that got me only close to the solution but not completely to it.
Here's what I'd like to do:
A column is sorted with a user-defined SortComparer. Its SortIndicator is off, the SortType may not be changed for this sortComparer.
However, if the user clicks this column it should be sorted by another user-defined SortComparer. From then on, clicking on the column changes the SortType. I'm aware that there is the solution of adding an invisible column and sorting this column by the first SortComparer, but my tree is quite large and this is the column that is always sorted by default so I'm fearing that this would slow down the whole program.
What I've already done is the following:
I handle the BeforeSortChange Event with the following Code
foreach (UltraTreeNodeColumn sortedColumn in e.SortedColumns) { if (sortedColumn.Key.Equals(Properties.Resources.ColumnNameKey) && sortedColumn.SortType == SortType.Descending && sortedColumn.SortComparer is NodeSorter) {
try { tree.EventManager.SetEnabled(TreeEventIds.BeforeSortChange, false); tree.EventManager.SetEnabled(TreeEventIds.AfterSortChange, false); UltraTreeNodeColumn nameColumn = tree.ColumnSettings.RootColumnSet.Columns[Properties.Resources.ColumnNameKey]; nameColumn.SortComparer = new StandardColumnSorter(); nameColumn.SortType = SortType.Ascending; nameColumn.ShowSortIndicators = DefaultableBoolean.True; // tree.Refresh(); this seems to cause some unreproducible update errors. } finally { tree.EventManager.SetEnabled(TreeEventIds.BeforeSortChange, true); tree.EventManager.SetEnabled(TreeEventIds.AfterSortChange, true); } } }
It nearly does everything that is required. My only problem is that the SortType will be Descending after the Sorting independent on the configured SortType in the BeforeSortChange Event.
I've also tried the approach you described above. But this led me to another problem. If the user changes the size of columns and depresses the left mouse button above this column header it will change it's sort type and possibly comparer which is very unexpected for the user.
Thanks in advance
Hi,
I'm having some trouble following what you are trying to do. So let me see if I understand this.
Basically, what you want is for clicking on the column header to toggle between two SortComparers instead of sorting ascending or descending.
And you want the sort indicator to always show the Ascending indicator or nothing depending on whether there is a SortComparer applied or not, respectively. So it will never show Descending.
Is that right?
Not completely. What I want is the following:
Let me define three states. State 1 is the default state when the user loads the tree for the first time. Transitions between states occur when the header is clicked
1. SortComparer = SortComparer1, sorted ascending, sort indicator not shown (default)
2. toggle Sort Comparer to SortComparer2, sorted ascending, sort indicator ASC shown
3. SortComparer is still SortComparer2, sorted descending, sort indicator DESC shown
So the first header click will lead from state 1 to state 2.
BUT all following clicks will change the state between 2 and 3 (2->3 or 3->2) (default behaviour of header click).
State 1 may only be reestablished by clicking a RestoreOriginalSortOrder button. I hope this helps you to understand the problem.
What I don't like about my solution so far is that the first click will lead from state 1 to state 3.
Edit: There also seems to be a problem with triggering a second sort inside the first one. Sometimes the tree will get corrupted.
I've found out that the tree corruptions didn't originate in this method.
I also solved the problem by moving the code from the BeforeSortChange event to the AfterSortChange event.
Thanks anyway