wpf - A trigger based on the value of a DataGridCell -
i have cells in datagrid , highlight cells in columns red when value 0. i'm not sure how approach this.
i've looked @ question: wpf: how highlight cells of datagrid meeting condition? none of solutions have worked me.
with using style triggers, seems triggers meant applied on properties. when nothing happens (i'm assuming because there's more content simple value).
with last suggested solution getting compile-time issue seemed manifestation of bug that's been in vs while now: custom binding class not working correctly
any ideas how can achieve this?
anyone have ideas?
the best way change background color of cell based on value of datagridcell define datatemplate datagridtemplatecolumn converter alter background color of cell. sample provided here uses mvvm.
the keys parts search in following example include:
1: xaml converts integer (factor) in model color:
<textblock text="{binding path=firstname}" background="{binding path=factor, converter={staticresource objectconvter}}" />
2: converter returns solidcolorbrush based on integer property in model:
public class objecttobackgroundconverter : ivalueconverter
3: viewmodel changes integer value in model between 0 , 1 button click fire event changes color in converter.
private void onchangefactor(object obj) { foreach (var customer in customers) { if ( customer.factor != 0 ) { customer.factor = 0; } else { customer.factor = 1; } } }
4: model implements inotifypropertychanged used fire event alter background color calling onpropertychanged
private int _factor = 0; public int factor { { return _factor; } set { _factor = value; onpropertychanged("factor"); } }
i've provided bits needed here in answer exception of core parts used foundation of mvvm pattern includes viewmodelbase (inotifypropertychanged) , delegatecommand can find in via google. note bind datacontext of view viewmodel in code-behind's constructor. can post these additional bits if needed.
here xaml:
<window x:class="datagridcellschangecolor.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:helpers="clr-namespace:datagridcellschangecolor.converter" title="mainwindow" height="350" width="525"> <window.resources> <helpers:objecttobackgroundconverter x:key="objectconvter"/> /window.resources> <grid> <grid.rowdefinitions> <rowdefinition height="auto"/> <rowdefinition/> </grid.rowdefinitions> <button content="change factor" command="{binding path=changefactor}"/> <datagrid grid.row="1" grid.column="0" background="transparent" itemssource="{binding customers}" isreadonly="true" autogeneratecolumns="false"> <datagrid.columns> <datagridtemplatecolumn header="first name" width="sizetoheader"> <datagridtemplatecolumn.celltemplate> <datatemplate> <textblock text="{binding path=firstname}" background="{binding path=factor, converter={staticresource objectconvter}}" /> </datatemplate> </datagridtemplatecolumn.celltemplate> </datagridtemplatecolumn> <datagridtemplatecolumn header="last name" width="sizetoheader"> <datagridtemplatecolumn.celltemplate> <datatemplate> <textbox text="{binding path=lastname}" /> </datatemplate> </datagridtemplatecolumn.celltemplate> </datagridtemplatecolumn> </datagrid.columns> </datagrid> </grid> </window>
here converter:
using system; using system.drawing; using system.globalization; using system.windows.data; using system.windows.media; using brushes = system.windows.media.brushes; namespace datagridcellschangecolor.converter { [valueconversion(typeof(object), typeof(solidbrush))] public class objecttobackgroundconverter : ivalueconverter { public object convert(object value, type targettype, object parameter, cultureinfo culture) { int c = (int)value; solidcolorbrush b; if (c == 0) { b = brushes.gold; } else { b = brushes.green; } return b; } public object convertback(object value, type targettype, object parameter, cultureinfo culture) { throw new notimplementedexception(); } } }
here viewmodel:
using system.collections.objectmodel; using system.windows.input; using datagridcellschangecolor.commands; using datagridcellschangecolor.model; namespace datagridcellschangecolor.viewmodel { public class mainviewmodel : viewmodelbase { public mainviewmodel() { changefactor = new delegatecommand<object>(onchangefactor, canchangefactor); } private observablecollection<customer> _customers = customer.getsamplecustomerlist(); public observablecollection<customer> customers { { return _customers; } } public icommand changefactor { get; set; } private void onchangefactor(object obj) { foreach (var customer in customers) { if ( customer.factor != 0 ) { customer.factor = 0; } else { customer.factor = 1; } } } private bool canchangefactor(object obj) { return true; } } }
here model:
using system; using system.collections.objectmodel; using datagridcellschangecolor.viewmodel; namespace datagridcellschangecolor.model { public class customer : viewmodelbase { public customer(string first, string middle, string last, int factor) { this.firstname = first; this.middlename = last; this.lastname = last; this.factor = factor; } public string firstname { get; set; } public string middlename { get; set; } public string lastname { get; set; } private int _factor = 0; public int factor { { return _factor; } set { _factor = value; onpropertychanged("factor"); } } public static observablecollection<customer> getsamplecustomerlist() { return new observablecollection<customer>(new customer[4] { new customer("larry", "a", "zero", 0), new customer("bob", "b", "one", 1), new customer("jenny", "c", "two", 0), new customer("lucy", "d", "three", 2) }); } } }
Comments
Post a Comment