datagrid - How to change the background color of a Silverlight DataGridRow? -
i have silverlight datagrid bound collection of myobjects
. myobject
has boolean field called ishighlighted
. change row's background color when value true. , have changed if becomes false.
i tried using loading_row
event (as explained here), didn't work me, event called once, , objetcs have boolean value set false @ time (it becomes truc when component selectes; works, checked values).
anybody has clue ? in advance !
update: made test application illustrate, reproduces problem.
<navigation:page x:class="aviews.tests" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" mc:ignorable="d" xmlns:navigation="clr-namespace:system.windows.controls;assembly=system.windows.controls.navigation" datacontext="{binding relativesource={relativesource self}}" d:designwidth="640" d:designheight="480" title="tests page"> <grid x:name="layoutroot"> <grid.rowdefinitions> <rowdefinition height="*" /> <rowdefinition height="auto"/> </grid.rowdefinitions> <sdk:datagrid grid.row="0" itemssource="{binding allitems, mode=twoway}" autogeneratecolumns="false" loadingrow="datagrid_loadingrow"> <sdk:datagrid.columns> <sdk:datagridtextcolumn binding="{binding value1}" header="value1" /> <sdk:datagridtextcolumn binding="{binding value2}" header="value2"/> <sdk:datagridcheckboxcolumn binding="{binding ishighlighted}" header="is highlighted" /> </sdk:datagrid.columns> </sdk:datagrid> <button content="change !" grid.row="1" horizontalalignment="left" click="button_click" /> </grid> </navigation:page>
public partial class tests : page, inotifypropertychanged { private sampleconverter bgconverter = new sampleconverter(); random r = new random(); public event propertychangedeventhandler propertychanged; protected void onpropertychanged(string name) { propertychangedeventhandler handler = this.propertychanged; if (handler != null) { handler(this, new propertychangedeventargs(name)); } } private observablecollection<sample> allitemsfield = new observablecollection<sample>(); public observablecollection<sample> allitems { { return this.allitemsfield; } set { if (this.allitemsfield != value) { this.allitemsfield = value; this.onpropertychanged("allitems"); } } } public tests() { initializecomponent(); } protected override void onnavigatedto(navigationeventargs e) { var tmp = enumerable.range(0, 100).select(f => new sample(f)).tolist(); foreach (var item in tmp) { item.propertychanged += new propertychangedeventhandler(item_propertychanged); } var coll = new observablecollection<sample>(tmp); this.allitems = coll; } void item_propertychanged(object sender, propertychangedeventargs e) { this.onpropertychanged("allitems"); } private void datagrid_loadingrow(object sender, datagridroweventargs e) { binding b = new binding("ishighlighted") { mode = bindingmode.oneway, converter = this.bgconverter, validatesonexceptions = true }; e.row.setbinding(datagridrow.backgroundproperty, b); } private void button_click(object sender, routedeventargs e) { foreach (var item in this.allitems) { item.ishighlighted = r.next(1000) % 2 == 0; } } }
public class sample: inotifypropertychanged { public event propertychangedeventhandler propertychanged; protected void onpropertychanged(string name) { propertychangedeventhandler handler = this.propertychanged; if (handler != null) { handler(this, new propertychangedeventargs(name)); } } private string value1field = string.empty; public string value1 { { return this.value1field; } set { if (this.value1field != value) { this.value1field = value; this.onpropertychanged("value1"); } } } private string value2field = string.empty; public string value2 { { return this.value2field; } set { if (this.value2field != value) { this.value2field = value; this.onpropertychanged("value2"); } } } private bool ishighlightedfield = false; public bool ishighlighted { { return this.ishighlightedfield; } set { if (this.ishighlightedfield != value) { this.ishighlightedfield = value; this.onpropertychanged("ishighlighted"); } } } public sample(int index) { this.value1 = string.format("value1 #{0}", index); this.value2 = string.format("value2 #{0}", index); } }
public class sampleconverter : ivalueconverter { public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { bool val = (bool)value; solidcolorbrush ret = val ? new solidcolorbrush(colors.red) : new solidcolorbrush(colors.green); return ret; } public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { throw new notimplementedexception(); } }
and result can seen on pictures:
when first arrive on page.
i click button, sets (random) values true. can see, binding updated, not ui.
i use scrollbar, go end, , come back, , oh! wonderful! rows correctly colored :-(
see below link solves problem changing control template of datagridrow
binds fill
property of backgroundrectangle
. can use boolconverter
convert ishighlighted
solidcolorbrush
.
Comments
Post a Comment