WPF Autogenerated DataGrid Cell changed event when bound to ItemSource -


i have simple datagrid has columns autogenerated , bound item source. item source updated @ intervals , can't find how fire event single cell changed. want change color of cell based on if update data source changed previous value of cell.

i looked @ highlighting cells in wpf datagrid when bound value changes http://codefornothing.wordpress.com/2009/01/25/the-wpf-datagrid-and-me/ still unsure how go implementing this. example code helpful started on right path.

if you're binding datatable, don't think productive path go down. doing kind of styling based on contents of datatable bound datagrid impossible in wpf. there several suggestions on stackoverflow, pretty hacky, event-driven (which bad news in wpf), , maintenance nightmare.

if however, itemssource binding observablecollection, rowviewmodel class represents data in single row of datagrid, shouldn't bad. make sure rowviewmodel implements inotifypropertychanged, , update individual rowviewmodels updated data. then, can add logic expose additional property on rowviewmodel indicates if particular value new - use styles/triggers in xaml set background color based on value of new property.

here's example of latter: if edit 1 of values in first column, turn cell red. same thing happen if change value in itemviewmodel programmatically via database update.

the xaml:

<window x:class="showgridupdates.mainwindow"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     title="mainwindow" height="350" width="525"> <grid>     <datagrid itemssource="{binding items}" autogeneratecolumns="false">         <datagrid.columns>             <datagridtextcolumn binding="{binding item1, updatesourcetrigger=propertychanged}">                 <datagridtextcolumn.cellstyle>                     <style targettype="datagridcell">                         <style.setters>                             <setter property="background" value="blue"/>                             <setter property="foreground" value="white"/>                         </style.setters>                         <style.triggers>                             <datatrigger binding="{binding item1changed}" value="true">                                 <setter property="background" value="red"/>                             </datatrigger>                         </style.triggers>                     </style>                 </datagridtextcolumn.cellstyle>             </datagridtextcolumn>             <datagridtextcolumn binding="{binding item2}"/>         </datagrid.columns>     </datagrid>    </grid> 

the code-behind:

public partial class mainwindow : window {     public mainwindow()     {         initializecomponent();         this.datacontext = new viewmodel();     } }  public class viewmodel : propertychangednotifier {     public viewmodel()     {         items = new observablecollection<itemviewmodel>()         {             new itemviewmodel(){item1="item1fistvalue", item2="item2firstvalue"},             new itemviewmodel(){item1="whocareswhatvalue", item2="icertainlydont"}         };          //just initial state correct         foreach (var item in items)         {             item.item1changed = false;         }     }      private observablecollection<itemviewmodel> _items;      public observablecollection<itemviewmodel> items     {                 {             return _items;         }         set         {             _items = value;             onpropertychanged("items");         }     } }  public class itemviewmodel : propertychangednotifier {     private string _item1;     private string _item2;      private bool _item1changed;      public bool item1changed     {                 {             return _item1changed;         }         set         {             _item1changed = value;             onpropertychanged("item1changed");         }     }      public string item1     {                 {             return _item1;         }         set         {             if (_item1 != value)                 item1changed = true;             else                 item1changed = false;              _item1 = value;             onpropertychanged("item1");         }     }      public string item2     {                 {             return _item2;         }         set         {             _item2 = value;             onpropertychanged("item2");         }     } }  public class propertychangednotifier : inotifypropertychanged {     public event propertychangedeventhandler propertychanged;     public void onpropertychanged(string propertyname)     {         var propertychanged = propertychanged;         if (propertychanged != null)         {             propertychanged(this, new propertychangedeventargs(propertyname));         }     } } 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -