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?
Hello,
After an investigation, I modified the previously attached sample. In order to have access to the Field itself I set the PreviewMouseRightButtonDown event for the grid LabelPresenter:
<igWPF:XamDataGrid.Resources> <Style TargetType="{x:Type igWPF:LabelPresenter}"> <EventSetter Event="PreviewMouseRightButtonDown" Handler="LP_PreviewMouseRightButtonDown"/> </Style> </igWPF:XamDataGrid.Resources>
In the event handler the clicked field could be accessed through the event argument Source property:
private void LP_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e) { Field field = ((Infragistics.Windows.DataPresenter.DataItemPresenter)e.Source).Field; 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; } MessageBox.Show("Field Name: " + field.Name + "\nLabel: " + field.Label); }
I have attached the modified sample application below. Please test it on your side and let me know if I may be of any further assistance.
Sincerely,
Teodosia Hristodorova
Associate Software Developer
5621.XamDataGrid_columnHighlight_onMouseRightClick.zip
Here in the sample, the "fieldName" actually is the field description.
How can I get the FieldName or Column Index if I click on the header?
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.
4578.XamDataGrid_columnHighlight_onMouseRightClick.zip