Forums - C# Corner

Forum guidelines
Dean

Dean

  • 1.3k
  • 3
  • 11.2k

WPF Datagrid Selected Row Styling

Apr 19 2011 9:25 AM
I have been trying for several hours to apply a custom style to the selected DataGridRow. I have found numerous examples for doing this in XAML however, this must be done in C# code directly as it is created at run time.

Firstly, I have managed to format the selected DataGridRow using the following code:

                // Create a trigger for when a row is selected
                Trigger newTrigger = new Trigger();
                newTrigger.Property = DataGridRow.IsSelectedProperty;
                newTrigger.Value = true;

                // Set the style for this trigger
                LinearGradientBrush selectedRowBackgroundBrush = new LinearGradientBrush();
                selectedRowBackgroundBrush.StartPoint = new Point(0.5, 0.0);
                selectedRowBackgroundBrush.EndPoint = new Point(0.5, 1.0);
                selectedRowBackgroundBrush.GradientStops.Add(new GradientStop(Colors.Coral, 0));
                selectedRowBackgroundBrush.GradientStops.Add(new GradientStop(Colors.Navy, 0.2)); 
                newTrigger.Setters.Add(new Setter(DataGridRow.BackgroundProperty, selectedRowBackgroundBrush));

                // Add the triggers to the row style
                rowStyle.Triggers.Add(newTrigger);
                newDataGrid.RowStyle = rowStyle;

However, the default selection style is still visible over top of my custom one. I have attempted to override the default style for the selected cell to make it invisible but have had no luck. The code i used is:

                // Remove the default styling when selecting a row
                Style newStyle = new Style();
                newStyle.TargetType = typeof(DataGridCell);

                Trigger newTrigger = new Trigger();
                newTrigger.Property = DataGridCell.IsSelectedProperty;
                newTrigger.Value = true;

                newTrigger.Setters.Add(new Setter(DataGridCell.BackgroundProperty, new SolidColorBrush(Color.FromArgb(0, 0, 0, 0)))); // Transparent
                newTrigger.Setters.Add(new Setter(DataGridCell.ForegroundProperty, new SolidColorBrush(Color.FromArgb(0, 0, 0, 0)))); // Transparent
                newTrigger.Setters.Add(new Setter(DataGridCell.BorderBrushProperty, new SolidColorBrush(Color.FromArgb(0, 0, 0, 0)))); // Transparent
                newTrigger.Setters.Add(new Setter(DataGridCell.BorderThicknessProperty, 0));

                newStyle.Triggers.Add(newTrigger);
                newDataGrid.CellStyle = newStyle;

As the default selection style is still visible so this code clearly doesn't work. Does anyone know what I am missing? Hopefully I'm not too far off in right field on this one. I wonder if i need to dive into templates. Also, my datagrid is databound to a datatable containing strings of all information for the datagrid. I wonder if the style I set at creation of the datagrid is not being applied to the new DataGridCells after the datagrid has been filled based on the databound source... Honestly though I have little experience with this and really no idea how to proceed so hopefully someone can point in the right direction.

Thanks!


Answers (1)

Visual Studio Live - Redmond 2016

SPONSORED BY

File APIs for .NET
Aspose are the market leader of .NET APIs for file business formats – natively work with DOCX, XLSX, PPT, PDF, MSG, MPP, images formats and many more!