Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6385311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:53:00+00:00 2026-05-25T02:53:00+00:00

How does one style a specific row or rows in a WPF Datagrid during

  • 0

How does one style a specific row or rows in a WPF Datagrid during runtime? Each change depends on some values of the shown data?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T02:53:01+00:00Added an answer on May 25, 2026 at 2:53 am

    I can’t tell from your question whether you are adding columns to your grid at run time, but either way you can add a CellStyle to the grid at design time that handles your specific styling needs using DataTriggers.

    For instance, the following would make all rows red where the Name property = “Billy Bob”:

        <DataGrid AutoGenerateColumns="True" Name="dataGrid1">
            <DataGrid.CellStyle>
                <Style TargetType="{x:Type DataGridCell}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Name}" Value="Billy Bob" >
                            <Setter Property="Background" Value="Red" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.CellStyle>
        </DataGrid>
    

    If you are adding columns programmatically at run time and you want to apply a certain style to them, you can still define those styles at design time in your xaml.

        <DataGrid AutoGenerateColumns="False" Name="dataGrid1">
            <DataGrid.Resources>
                <Style TargetType="{x:Type DataGridCell}" x:Key="MyCellStyle">
                    <Setter Property="Foreground" Value="Green"/>
                </Style>
            </DataGrid.Resources>
            ...
    

    Then when you are adding the columns you can apply that style to them:

    col.CellStyle = (Style)dataGrid1.Resources("MyCellStyle");
    

    Update

    If you have a list of songs and you want to change the row color of every song that has an artist whose name starts with an “a”, then you could use an IValueConverter.

    The following converter would do the trick:

    public class ArtistNameConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, 
            object parameter, CultureInfo culture)
        {
            try
            {
                return value.ToString().StartsWith(parameter.ToString());
            }
            catch
            {
                return false;
            }
        }
    
        public object ConvertBack(object value, Type targetType, 
            object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    Then you could use the converter in your xaml like so:

        <DataGrid AutoGenerateColumns="True" Name="dataGrid1">
            <DataGrid.Resources>
                <converters:ArtistNameConverter x:Key="ArtistNameConverter"></converters:ArtistNameConverter>
            </DataGrid.Resources>
            <DataGrid.CellStyle>
                <Style TargetType="{x:Type DataGridCell}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ArtistName, Converter={StaticResource ArtistNameConverter}, ConverterParameter=a}" Value="True" >
                            <Setter Property="Background" Value="Red" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.CellStyle>
        </DataGrid>
    

    Notice how I am passing an “a” into the converter as the parameter. You can pass in whatever letter you want, and the rows that have artists that start with that letter will have their background color set to red.

    Update 2

    If you want to pass in a variable of some sort to the converter, you can use MultiBinding.

    The Converter would look like this:

    public class ArtistNameConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, 
            System.Globalization.CultureInfo culture)
        {
            try
            {
                return values[0].ToString().StartsWith(values[1].ToString());
            }
            catch
            {
                return false;
            }
        }
    
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, 
            System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    The first parameter passed in is the artist name, the second is the letter.

    And you would use it in your grid like this:

            <DataGrid.CellStyle>
                <Style TargetType="{x:Type DataGridCell}">
                    <Style.Triggers>
                        <DataTrigger Value="True" >
                            <DataTrigger.Binding>
                                <MultiBinding Converter="{StaticResource ArtistNameConverter}">
                                    <Binding Path="ArtistName" />
                                    <Binding Mode="OneWay" ElementName="FirstLetter" Path="Text" />
                                </MultiBinding>
                            </DataTrigger.Binding>
                            <Setter Property="Background" Value="Red" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.CellStyle>
    

    In this example, the first letter is coming from the “Text” property of a control called “FirstLetter”. You can change that binding to whatever you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How does one style links for the current page differently from others? I would
does one perform better over the other in terms of indexing/quering etc ? e.g.
How does one go about authoring a Regular Expression that matches against all strings
How does one go about referencing a class's static properties in xaml? In other
How does one convert an image from one color profile to another (screen to
How does one go about programatically building a TemplateColumn object and adding it to
How does one handle a DateTime with a NOT NULL ? I want to
How does one invoke a groovy method that prints to stdout, appending the output
How does one start development in Silverlight? Does one need a new IDE? or
How does one do this? If I want to analyze how something is getting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.