protected void defaultSort()
        {
            if (ItemsSource == null)
            {
                return;
            }

            this.SelectedIndex = 0;

            GridView gridView = this.View as GridView;

            if (gridView != null)
            {
                SortableGridViewColumn sortableGridViewColumn = null;

                foreach (GridViewColumn gridViewColumn in gridView.Columns)
                {
                    sortableGridViewColumn = gridViewColumn as SortableGridViewColumn;

                    if (sortableGridViewColumn != null)
                    {
                        if (sortableGridViewColumn.IsDefaultSortColumn)
                        {
                            break;
                        }

                        sortableGridViewColumn = null;
                    }
                }

                if (sortableGridViewColumn != null)
                {
                    lastSortedOnColumn = sortableGridViewColumn;

                    Sort(sortableGridViewColumn.SortPropertyName, ListSortDirection.Ascending);



                    if (!String.IsNullOrEmpty(this.ColumnHeaderSortedAscendingTemplate))
                    {
                        sortableGridViewColumn.HeaderTemplate = this.TryFindResource(ColumnHeaderSortedAscendingTemplate) as DataTemplate;
                    }



                    this.SelectedIndex = 0;
                }
            }
        }
        private void GridViewColumnHeaderClickedHandler(object sender, RoutedEventArgs e)
        {
            GridViewColumnHeader headerClicked = e.OriginalSource as GridViewColumnHeader;


            if (headerClicked != null && headerClicked.Role != GridViewColumnHeaderRole.Padding)
            {
                SortableGridViewColumn sortableGridViewColumn = (headerClicked.Column) as SortableGridViewColumn;

                if (sortableGridViewColumn != null && !String.IsNullOrEmpty(sortableGridViewColumn.SortPropertyName))
                {
                    ListSortDirection direction;

                    bool newSortColumn = false;

                    if (lastSortedOnColumn == null

                        || String.IsNullOrEmpty(lastSortedOnColumn.SortPropertyName)

                        || !String.Equals(sortableGridViewColumn.SortPropertyName, lastSortedOnColumn.SortPropertyName, StringComparison.InvariantCultureIgnoreCase))
                    {
                        newSortColumn = true;

                        direction = ListSortDirection.Ascending;
                    }

                    else
                    {
                        if (lastDirection == ListSortDirection.Ascending)
                        {
                            direction = ListSortDirection.Descending;
                        }

                        else
                        {
                            direction = ListSortDirection.Ascending;
                        }
                    }

                    string sortPropertyName = sortableGridViewColumn.SortPropertyName;

                    Sort(sortPropertyName, direction);



                    if (direction == ListSortDirection.Ascending)
                    {
                        if (!String.IsNullOrEmpty(this.ColumnHeaderSortedAscendingTemplate))
                        {
                            sortableGridViewColumn.HeaderTemplate = this.TryFindResource(ColumnHeaderSortedAscendingTemplate) as DataTemplate;
                        }

                        else
                        {
                            sortableGridViewColumn.HeaderTemplate = null;
                        }
                    }

                    else
                    {
                        if (!String.IsNullOrEmpty(this.ColumnHeaderSortedDescendingTemplate))
                        {
                            sortableGridViewColumn.HeaderTemplate = this.TryFindResource(ColumnHeaderSortedDescendingTemplate) as DataTemplate;
                        }

                        else
                        {
                            sortableGridViewColumn.HeaderTemplate = null;
                        }
                    }

                    if (newSortColumn && lastSortedOnColumn != null)
                    {
                        if (!String.IsNullOrEmpty(this.ColumnHeaderNotSortedTemplate))
                        {
                            lastSortedOnColumn.HeaderTemplate = this.TryFindResource(ColumnHeaderNotSortedTemplate) as DataTemplate;
                        }

                        else
                        {
                            lastSortedOnColumn.HeaderTemplate = null;
                        }
                    }

                    lastSortedOnColumn = sortableGridViewColumn;
                }
            }
        }