c# - wpf datagrid foreground binding to item's property programmaticaly -


i have wpf datagrid control runtime generated columns. itemssource set generic list of examineexampledatagriditem:

public class examineexamplesdatagriditem {     public list<string> predictiveattributevalues { get; set; }     public string decisiveattributevalue { get; set; }     public string examinedattributevalue { get; set; }      public brush examinedattributevaluecolor     {         // if set debug point here, i'll there, makes no sense                 {             return decisiveattributevalue == null || examinedattributevalue == null                 ? brushes.black                 : decisiveattributevalue.equals(examinedattributevalue) ? brushes.green : brushes.red;         }         set { }     } } 

when binding fields datagrid instance, works fine except coloring:

list<examineexamplesdatagriditem> items = new list<examineexamplesdatagriditem>();  // several columns, works fine (int = 0; < _attributetypeset.predictiveattributetypes.count; i++) {     datagridcomboboxcolumn attributetypecolumn = new datagridcomboboxcolumn()     {         textbinding = new binding("predictiveattributevalues[" + + "]")     };     datagrid.columns.add(attributetypecolumn); }  // prev last column, works fine datagridcomboboxcolumn decisivecomboboxcolumn = new datagridcomboboxcolumn() {     cellstyle =         new style(typeof (datagridcell)) {setters = {new setter() {property = foregroundproperty, value = brushes.blue}}} }; datagrid.columns.add(decisivecomboboxcolumn);  // last column, content want color datagridtextcolumn examinedcolumn = new datagridtextcolumn() {     elementstyle = new style()     {         targettype = typeof(textblock),         setters =         {             // works, but..             // new setter(textblock.foregroundproperty, brushes.red)              // ... need one. calls examinedattributevaluecolor property, displays black color anyway             new setter(textblock.foregroundproperty, new binding("examinedattributevaluecolor"))         }     } }; datagrid.columns.add(examinedcolumn);  datagrid.itemssource = items; 

when populating itemssource color stays black. tried bind cellstyle instead of elementstyle this:

cellstyle = new style() {     targettype = typeof(datagridcell),     setters =     {         new setter(datagridcell.foregroundproperty, new binding("examinedattributevaluecolor"))     } } 

but doesn't work, elementstyle. found lot of troubleshoots, xaml coding. possible want c# code? how should it? should use other cellstyle or elementstyle property? saw fontstyle property of datagridtextcolumn, know defines font family.

here quick , basic example of setting foreground , background of cells in datagrid plus adding combobox drop-down collection of items, following mvvm pattern

first create basepropertychanged class implements inotifypropertychanged, inherited wherever want change property value notify ui

public class basepropertychanged : inotifypropertychanged {     public event propertychangedeventhandler propertychanged;      public void notifypropertychanged(string info)     {         if (propertychanged != null)         {             propertychanged(this, new propertychangedeventargs(info));         }     } } 

here 'examineexamplesdatagriditem' class, had make here cos don't know yours looks like.....

public class examineexamplesdatagriditem : basepropertychanged {       public examineexamplesdatagriditem()     {         predictiveattributevalues = new list<string>();         predictiveattributevalues.add("predict 1");         predictiveattributevalues.add("predict 2");         predictiveattributevalues.add("predict 3");         predictiveattributevalues.add("predict 4");         predictiveattributevalues.add("predict 5");     }      private list<string> _predictiveattributevalues;      public list<string> predictiveattributevalues     {         { return _predictiveattributevalues; }         set { _predictiveattributevalues = value; notifypropertychanged("predictiveattributevalues"); }     }      private string _coll1;      public string coll1     {         { return _coll1; }         set { _coll1 = value; notifypropertychanged("coll1"); }     }      private string _coll2;      public string coll2     {         { return _coll2; }         set { _coll2 = value; notifypropertychanged("coll2"); }     } } 

}

here viewmodel view (xaml) binds to, task maintain state of data in application, when data changes (here in viewmodel) result of inputs user or updates external source (i.e. data base) view updated (using binding , datacontext)

class base_viewmodel : basepropertychanged {      private list<examineexamplesdatagriditem> _items;      public list<examineexamplesdatagriditem> items     {         { return _items; }         set { _items = value; notifypropertychanged("items"); }     }      public base_viewmodel()     {         items = new list<examineexamplesdatagriditem>();         items.add(new examineexamplesdatagriditem() { coll1 = "row 1 col 1", coll2 = "row 1 col 2" });         items.add(new examineexamplesdatagriditem() { coll1 = "row 2 col 1", coll2 = "row 2 col 2" });         items.add(new examineexamplesdatagriditem() { coll1 = "row 3 col 1", coll2 = "row 3 col 2" });     } } 

here xaml, notice how style, setters , binding done in xaml...

<grid>      <datagrid name="datagrid1" autogeneratecolumns="false" itemssource="{binding items}" >         <datagrid.columns>             <datagridtemplatecolumn header="combobox">                 <datagridtemplatecolumn.celltemplate>                     <datatemplate>                         <combobox selectedindex="0" itemssource="{binding predictiveattributevalues}" />                     </datatemplate>                 </datagridtemplatecolumn.celltemplate>              </datagridtemplatecolumn>             <datagridtextcolumn binding="{binding coll1}" header="column 1">                 <datagridtextcolumn.cellstyle>                     <style targettype="datagridcell">                         <setter property="background"  value="blue" />                         <setter property="foreground" value="white" />                     </style>                 </datagridtextcolumn.cellstyle>             </datagridtextcolumn>             <datagridtextcolumn binding="{binding coll2}" header="column 2">                 <datagridtextcolumn.cellstyle>                     <style targettype="datagridcell">                         <setter property="background"  value="red" />                         <setter property="foreground" value="white" />                     </style>                 </datagridtextcolumn.cellstyle>             </datagridtextcolumn>         </datagrid.columns>     </datagrid>  </grid> 

and lastly data context..... now, have set in code-behind merely simplify example ordinarily 1 line of code go in xaml need more complex xaml code because need add reference viewmodel in xaml using application name , don't know [your] application call or folder structure

// in code - behind file of wpf window initializecomponent(); // set data context view this.datacontext = new base_viewmodel(); 

it looks lot of code know, when break down there isn't there really. thing free style , modify ui controls in xaml.... final test this.... in pure mvvm application should able comment out ui controls (in xaml) , code still compiles without errors.....


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -