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
550
Change column header
posted

Hi,

i am trying to change the column header text on right click. How do i get the Current column on the grid on mouse right click event.

Parents
No Data
Reply
  • 6912
    Suggested Answer
    posted

    Hi,

    You can try the following snippet:

    ...
    this.XGrid.MouseRightButtonUp += new System.Windows.Input.MouseButtonEventHandler(XGrid_MouseRightButtonUp);
    this.XGrid.MouseRightButtonDown +=new System.Windows.Input.MouseButtonEventHandler(XGrid_MouseRightButtonDown);
    ...
    private void XGrid_MouseRightButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        System.Windows.Point p = e.GetPosition(Application.Current.RootVisual);
    
        IEnumerable<HeaderCellControl> elements =
            VisualTreeHelper.FindElementsInHostCoordinates(p, Application.Current.RootVisual).OfType<HeaderCellControl>();
    
        HeaderCellControl headerCellControl = elements.FirstOrDefault();
    
        if (headerCellControl != null)
        {
            headerCellControl.Cell.Column.HeaderText = "Right clicked!";
            e.Handled = true;
        }
    }
    
    private void XGrid_MouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        e.Handled = true;
    }
    ...
    

    HTH

Children