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 3313326
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:06:08+00:00 2026-05-17T22:06:08+00:00

I have a DataGrid in my Silverlight application that I would like to highlight

  • 0

I have a DataGrid in my Silverlight application that I would like to highlight an entire column when that column is sorted.

It’s similar in concept to this previous question: Silverlight DataGrid: Highlight an entire column.

Any ideas? I’ve thought maybe attaching a behavior to the LayoutUpdated event and checking a cell to see if it’s in a sorted column?

  • 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-05-17T22:06:09+00:00Added an answer on May 17, 2026 at 10:06 pm

    There are several issues that you have to get around to do what you want to do. The biggest issue is that finding out when a column is sorted is a lot harder then it should be. The only way I have ever found to identify that a column was sorted to make DataGrid.ItemsSource a PagedCollectionView and listen to the CollectionChanged event on its SortDescriptions property. If you use a PagedCollectionView for your ItemsSource and all columns that can be sorted on are DataGridBoundColumn (really any of the premades other than TemplateColumn) then this behavior should be fairly close to what you want to do.

    public class DataGridSortedColumnHighlightBehavior : Behavior<DataGrid>
    {
    
        private List<DataGridRow> rows = new List<DataGridRow>();
    
        protected override void OnAttached()
        {
            base.OnAttached();
    
            AssociatedObject.LoadingRow += AssociatedObject_LoadingRow;
            AssociatedObject.UnloadingRow += AssociatedObject_UnloadingRow;
    
            if (AssociatedObject.ItemsSource == null)
            {
                AssociatedObject.LayoutUpdated += AssociatedObject_LayoutUpdated;
            }
            else
            {
                var collection =
                    ((AssociatedObject.ItemsSource as PagedCollectionView).SortDescriptions as INotifyCollectionChanged);
                collection.CollectionChanged += DataGridSortedColumnHighlightBehavior_CollectionChanged;
            }
        }
    
        void AssociatedObject_UnloadingRow(object sender, DataGridRowEventArgs e)
        {
            rows.Remove(e.Row);
        }
    
        void AssociatedObject_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            rows.Add(e.Row);
        }
    
        private void AssociatedObject_LayoutUpdated(object sender, EventArgs e)
        {
            if (AssociatedObject.ItemsSource != null)
            {
                AssociatedObject.LayoutUpdated -= AssociatedObject_LayoutUpdated;
                var collection =
                    ((AssociatedObject.ItemsSource as PagedCollectionView).SortDescriptions as INotifyCollectionChanged);
                collection.CollectionChanged += DataGridSortedColumnHighlightBehavior_CollectionChanged;
            }
        }
    
        void DataGridSortedColumnHighlightBehavior_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if(e.NewItems != null)
            {
                foreach(SortDescription sortDesc in e.NewItems)
                {
                    foreach(var column in AssociatedObject.Columns)
                    {
                        var boundColumn = column as DataGridBoundColumn;
                        if (boundColumn == null)
                            continue;
                        if (boundColumn.Binding.Path.Path == sortDesc.PropertyName)
                        {
                            foreach (var row in rows)
                                ColorCell(column,row,Colors.Red);
                        }
                    }
                }
            }
            if(e.OldItems != null)
            {
                foreach(SortDescription sortDesc in e.OldItems)
                {
                    foreach(var column in AssociatedObject.Columns)
                    {
                        var boundColumn = column as DataGridBoundColumn;
                        if (boundColumn == null)
                            continue;
                        if (boundColumn.Binding.Path.Path == sortDesc.PropertyName)
                        {
                            foreach (var row in rows)
                                ColorCell(column,row,Colors.White);
                        }
                    }
                }
            }
        }
    
        private void ColorCell(DataGridColumn column, DataGridRow item, Color color )
        {
            var content = column.GetCellContent(item);
            if (content == null)
                return;
    
            var parent = GetParent<DataGridCell>(content);
    
            if (parent != null)
                parent.Background = new SolidColorBrush(color);
    
        }
    
        public static T GetParent<T>(DependencyObject source)
            where T : DependencyObject
        {
            DependencyObject parent = VisualTreeHelper.GetParent(source);
            while (parent != null && !typeof(T).IsAssignableFrom(parent.GetType()))
            {
                parent = VisualTreeHelper.GetParent(parent);
            }
            return (T)parent;
        }        
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a DataGrid in my Silverlight application, and would like to highlight an
I have a DataGrid where each column has a SortExpression. I would like the
I have a Silverlight application that is using a DataGrid. Inside of that DataGrid
Continuing my problem from yesterday, the Silverlight datagrid I have from this issue is
I have a DataGrid, with an ItemTemplate that has an image and label. In
ASP.NET 1.1 - I have a DataGrid on an ASPX page that is databound
The Scenario Bit: On one of the controls within my Silverlight application I have
I'm using Silverlight Beta 4 for a LOB application. After finding out today that
Following on from this initial investigations on Silverlight architectures, I have some new requirements
I have a datagrid getting bound to a dataset, and I want to display

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.