Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
90
Highlight a whole column by right-clicking on the column header
posted

I have an xamDataGrid.

I right-click on a column header, I want to the whole column highlighted (not selected) to a color (for example, the background turns to light blue.) How can I achieve it?

Parents
No Data
Reply
  • 1560
    Offline posted

    Hello,

    I have been looking into your requirement and my suggestion is to handle the XamDataGrid's MouseRightButtonDown event where you can check if the element that is clicked is Label. In this case, its TemplatedParent Name value would be "LabelContent" and its Content property would contain the actual field name. You can access this field in your XamDataGrid by this name and set its CellValuePresenterStyle to already defined style per your requirement:

     public partial class MainWindow : Window
        {
            Style myStyle;
            public MainWindow()
            {
                InitializeComponent();
                myStyle = new Style(typeof(CellValuePresenter));
                myStyle.Setters.Add(new Setter(CellValuePresenter.BackgroundProperty, Brushes.LightBlue));
                
            }
     
            private void XamDataGrid1_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
            {
               System.Windows.FrameworkElement templatedParent = ((System.Windows.FrameworkElement)((System.Windows.FrameworkElement)e.OriginalSource).TemplatedParent).TemplatedParent as System.Windows.FrameworkElement;
                if(templatedParent != null)
                {
                    if (templatedParent.Name == "LabelContent")
                    {
                        string fieldName = ((System.Windows.Controls.ContentControl)templatedParent).Content.ToString();
                        if(this.xamDataGrid1.FieldLayouts[0].Fields[fieldName] != null)
                        {
                            Field field = this.xamDataGrid1.FieldLayouts[0].Fields[fieldName];
                            if(field.Settings.CellValuePresenterStyle == null)
                            {
                                field.Settings.CellValuePresenterStyle = myStyle;
                            }
                            else
                            {
                                //if you right click again it will reset the applied style
                                field.Settings.CellValuePresenterStyle = null;
                            }
                           
                        }
                    }
                }
           
            }
        }

    Additionally, in order to be able to reset this highlight if you right click again on the already marked field, I added checking if the CellValuePresenterStyle is set and in this case reset its value back to null.

    I have attached a sample application, that uses this approach.    

    Please test it on your side and let me know if I may be of any further assistance.

    Sincerely,

    Teodosia Hristodorova

    Associate Software Developer

    4578.XamDataGrid_columnHighlight_onMouseRightClick.zip

Children