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

  • Home
  • SEARCH
  • 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 8653859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:38:38+00:00 2026-06-12T14:38:38+00:00

I have created a datagrid in WPF which has cell values continuously changing, But

  • 0

I have created a datagrid in WPF which has cell values continuously changing, But I am not able to change the backcolor of a the cells with respect to the previous value of cell,
for Example if the value of the cell changes from 10 to 20 the the color of the cell should become green indicating that the value increased and if the value becomes 5, the cell backcolor should become red indicating that the value has decreased. I have done that in winforms on cellvaluechange event of datagridview, but in WPF I am not able to do the same. any of the professionals have done anything regarding that.

  • 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-06-12T14:38:40+00:00Added an answer on June 12, 2026 at 2:38 pm

    Create a property IsIncreased in your cell’s DataContext to hold a Nullable<bool> according to increasing/decreasing.

    public class DatagridCellViewModel: INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected void FirePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    
        private bool _isIncreased
        public bool? IsIncreased
        {
          get{ return _isIncreased; }
          set{ 
              _isIncreased = value, 
              FirePropertyChanged("IsIncreased");
             }
        }
    
        //this is going to store the value you have in your table. Don't forget to bind it to the cells content
        private int _dataValue;
        public int DataValue
        {
          get{ return _dataValue; }
          set{
                if(_dataValue != value)
                {
                   IsIncreased = _dataValue < value
                   _dataValue= value;
    
                   FirePropertyChanged("DataValue");
                }             
             }
        }
    
        //other properties and methods
    }
    
    public class DataGridViewModel
    {
    
         private ICollection<DataGridRowViewModel> _myDgRows = new ObservableCollection<DataGridRowViewModel>();
         public ObservableCollection<DataGridRowViewModel> MyDgRows { get{ return _myDgRows;}
    }
    
    public class DataGridRowViewModel : INotifyPropertyChanged // don't forget to implement this interface
    {
         //put here fields and properties you want to display in a row in your datagrid
         //that means as many fields as many columns you have
    
        private DataGridCellViewModel _cellValue;
        public int CellValue
        {
          get{ return _cellValue; }
          set{
                if(!_cellValue.Equals(value))
                {
                   _cellValue= value;
    
                   FirePropertyChanged("CellValue");
                }             
             }
        }
    }
    

    Create a DataTrigger to set the background of the cell according to the IsIncreased value:

    <DataGrid ItemsSource="{Binding MyDgRows}" AutoGenerateColumns="False" >
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="ChangingValues" Width="SizeToCells">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBox Name="ContentTbx" Text="{Binding CellValue.DataValue}" />
                   <DataTemplate.Triggers>
                      <DataTrigger Binding="{Binding CellValue.IsIncreased}" Value="True">
                         <Setter Property="Background" Value="Green" />
                      </DataTrigger>
                     <DataTrigger Binding="{Binding CellValue.IsIncreased}" Value="False">
                         <Setter TargetName="ContentTbx" Property="Background" Value="Red"/>
                     </DataTrigger>
                   </DataTemplate.Triggers>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
    </DataGrid>
    

    Set the IsIncreased prop every time when the cells Data prop changed

    EDIT : Don’t bind table but an ObservableCollecition calls MyDgRows. For this a DataGridViewModel instance should be your DataGrids DataContext.

    Probably you need to deal with int-string conversion in the DataValue prop.

    For that matter I googled

    datagrid wpf

    the first link was this

    and

    datatriggers datatemplate

    the second result was this

    and they covers almost the whole issue. Be more self-sufficient

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

Sidebar

Related Questions

I have created a checkbox column in a WPF DataGrid, and I have set
I have a combobox inside of my WPF DataGrid. It is created like this:
I have created a WPF tooklit datagrid in C# and the ItemsSource is set
I'm using the WPF toolkit datagrid. I have it set to SelectionUnit=Cell and SelectionMode=Extended
I have a WPF Datagrid populated using a Dataset. I am trying to change
I have created WPF MVVM application, and set WPFToolkit DataGrid binding to DataTable so
I have a WPF DataGrid templatecolumn that has a DataTemplate for an AutoCompleteBox from
I have created a custom style for my WPF datagrid by overriding its control
I have created a nested WPF Datagrid. I see you can expand it by
I have followed this tutorial which allowed me to create a Silverlight DataGrid that

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.