Forum guidelines
  • Home
  • »
  • WPF
  • »
  • WPF Datagrid Selected Row Styling
AuthorQuestion
WPF Datagrid Selected Row Styling
Posted on: 19 Apr 2011
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!


AuthorReply
Re: WPF Datagrid Selected Row Styling
Posted on: 12 May 2011  
For row selection from a datagrid try this:

for (int i = 0; i < dataGrid.Items.Count; i++)
{
    DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(i);
    TextBlock cellContent = dataGrid.Columns[0].GetCellContent(row) as TextBlock;
    if (cellContent != null && cellContent.Text.Equals(textBox1.Text))
    {
        object item = dataGrid.Items[i];
        dataGrid.SelectedItem = item;
        dataGrid.ScrollIntoView(item);
        row.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
        break;
    }
}

And for styling see the link below:

http://blogs.msdn.com/b/jaimer/archive/2009/01/20/styling-microsoft-s-wpf-datagrid.aspx





 

"Nothing is Impossible If you Believe in Yourself"   Nipun Tomar.

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.