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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:59:09+00:00 2026-05-16T20:59:09+00:00

I wish to highlight the columns on a WPF datagrid as the mouse moves.

  • 0

I wish to highlight the columns on a WPF datagrid as the mouse moves. Some of the problems I face are:

  • Getting the coordinates of columns to test for when the mouse is over them
  • Changing the background color of a column

Any help will be much appreciated.

  • 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-16T20:59:09+00:00Added an answer on May 16, 2026 at 8:59 pm

    How about updating the style for the DataGridCell and defining an “IsMouseOver” trigger in it? Something like this:

    <DataGrid x:Name="dg">
        <DataGrid.Resources>
            <Style x:Key="{x:Type DataGridCell}" TargetType="{x:Type DataGridCell}">
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter Property="Background" Value="LightGray"/>
                    </Trigger>
                    <Trigger Property="IsSelected" Value="true">
                        <Setter Property="Background" Value="Blue" />
                        <Setter Property="Foreground" Value="White" />
                        <Setter Property="BorderBrush" Value="Black" />
                    </Trigger>
                    <Trigger Property="IsKeyboardFocusWithin" Value="true">
                        <Setter Property="BorderBrush" Value="Gray" />
                    </Trigger>
                </Style.Triggers>
                <Setter Property="Background" Value="Transparent" />
                <Setter Property="BorderBrush" Value="Transparent" />
                <Setter Property="BorderThickness" Value="1" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type DataGridCell}">
                            <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" 
                                    BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="true">
                                <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </DataGrid.Resources>
    </DataGrid>
    

    EDIT:

    Below is a solution that: 1) highlights the currently hovered cell, 2) highlights the entire row, and 3) highlights the entire column. I just coded this up quickly and haven’t tested it thoroughly so you might encounter some issues. But anyway, this should give you an idea on how they can be done.

    My solution for highlighting the entire column involves the use of two attached properties that were being assigned to the DataGridCell when the mouse is over it. Just check it out and see if it works for you.

    XAML:

    <Window x:Class="StackOverflow.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:StackOverflow"
            Title="MainWindow" Height="350" Width="525">
        <DataGrid x:Name="dg" AutoGenerateColumns="True">
            <DataGrid.Resources>
                <Style x:Key="{x:Type DataGridCell}" TargetType="{x:Type DataGridCell}">
                    <Setter Property="Background" Value="Transparent" />
                    <Setter Property="BorderBrush" Value="Transparent" />
                    <Setter Property="BorderThickness" Value="1" />
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type DataGridCell}">
                                <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" 
                                    BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="true">
                                    <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                    <Style.Triggers>
                        <Trigger Property="local:DataGridBehavior.IsCellHighlighted" Value="True">
                            <Setter Property="Background" Value="LightGray"/>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="local:DataGridBehavior.HighlightColumn" Value="True"/>
                            <Setter Property="Background" Value="Green"/>
                        </Trigger>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter Property="Background" Value="Blue" />
                            <Setter Property="Foreground" Value="White" />
                            <Setter Property="BorderBrush" Value="Black" />
                        </Trigger>
                        <Trigger Property="IsKeyboardFocusWithin" Value="true">
                            <Setter Property="BorderBrush" Value="Gray" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
                <Style TargetType="{x:Type DataGridRow}">
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="LightGray"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.Resources>
        </DataGrid>
    </Window>
    

    DataGridBehavior class:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Interactivity;
    using System.Windows.Controls;
    using System.Windows.Media;
    using System.Windows.Controls.Primitives;
    
    namespace StackOverflow
    {
        public class DataGridBehavior : DependencyObject
        {
    
            public static bool GetHighlightColumn(DependencyObject obj)
            {
                return (bool)obj.GetValue(HighlightColumnProperty);
            }
    
            public static void SetHighlightColumn(DependencyObject obj, bool value)
            {
                obj.SetValue(HighlightColumnProperty, value);
            }
    
            // Using a DependencyProperty as the backing store for HighlightColumn.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty HighlightColumnProperty =
                DependencyProperty.RegisterAttached("HighlightColumn", typeof(bool), 
                typeof(DataGridBehavior), new FrameworkPropertyMetadata(false, OnHighlightColumnPropertyChanged));
    
    
    
            public static bool GetIsCellHighlighted(DependencyObject obj)
            {
                return (bool)obj.GetValue(IsCellHighlightedProperty);
            }
    
            public static void SetIsCellHighlighted(DependencyObject obj, bool value)
            {
                obj.SetValue(IsCellHighlightedProperty, value);
            }
    
            // Using a DependencyProperty as the backing store for IsCellHighlighted.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty IsCellHighlightedProperty =
                DependencyProperty.RegisterAttached("IsCellHighlighted", typeof(bool), typeof(DataGridBehavior), 
                new UIPropertyMetadata(false));
    
    
    
            private static void OnHighlightColumnPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
            {
                Console.WriteLine(e.NewValue);
                DataGridCell cell = sender as DataGridCell;
    
                if (cell != null) 
                {
                    DataGrid dg = GetDataGridFromCell(cell);
                    DataGridColumn column = cell.Column;
    
                    for (int i = 0; i < dg.Items.Count; i++) 
                    {
                        DataGridRow row = dg.ItemContainerGenerator.ContainerFromIndex(i) as DataGridRow;
                        DataGridCell currentCell = GetCell(row, column);
                        if (currentCell != null)
                        {
                            currentCell.SetValue(DataGridBehavior.IsCellHighlightedProperty, e.NewValue);
                        }
                    }
    
                }
            }
    
            private static DataGrid GetDataGridFromCell(DataGridCell cell)
            { 
                DataGrid retVal = null;
                FrameworkElement fe = cell;
                while ((retVal == null) && (fe != null))
                {
                    if (fe is DataGrid)
                        retVal = fe as DataGrid;
                    else
                        fe = VisualTreeHelper.GetParent(fe) as FrameworkElement;
                }
                return retVal;
            }
    
            private static DataGridCell GetCell(DataGridRow row, DataGridColumn column) 
            {
                DataGridCell retVal = null;
                DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);
                if (presenter != null)
                {
                    for (int i = 0; i < presenter.Items.Count; i++)
                    {
                        DataGridCell cell = presenter.ItemContainerGenerator.ContainerFromIndex(i) as DataGridCell;
                        if ((cell != null) && (cell.Column == column))
                        {
                            retVal = cell;
                            break;
                        }
                    }
                }
    
                return retVal;
            }
    
            private static T GetVisualChild<T>(Visual parent) where T : Visual
            {
                T child = default(T);
                int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
                for (int i = 0; i < numVisuals; i++)
                {
                    Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
                    child = v as T;
                    if (child == null)
                    {
                        child = GetVisualChild<T>(v);
                    }
                    if (child != null)
                    {
                        break;
                    }
                }
                return child;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wish to test a function that will generate lorem ipsum text, but it
I'm working on a web form where I wish to (after form submission) highlight
I wish Subversion had a better way of moving tags. The only way that
I wish to know all the pros and cons about using these two methods.
I wish to implement a 2d bit map class in Python. The class would
I wish to implement my software on a shareware basis, so that the user
I wish I were a CSS smarty .... How can you place a div
I wish to search a database table on a nullable column. Sometimes the value
I wish to use xml and xsl to generate controls on an asp.net page.
I wish to perform an experiment many different times. After every trial, I am

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.