i getting following exception when attempt change background color of border using 2 way data binding.
{"must create dependencysource on same thread dependencyobject."} when first got error changed code have colors static resources hoping solve problem, didn't work.
xaml
<grid x:name="bggrid"> <grid.resources> <color x:key="acgreen">#ff0a7e07</color> <color x:key="acyellow">#ffe2bd00</color> <color x:key="acred">#ffaf0b01</color> <solidcolorbrush x:key="greenbrush" color="{staticresource acgreen}" /> <solidcolorbrush x:key="yellowbrush" color="{staticresource acyellow}" /> <solidcolorbrush x:key="redbrush" color="{staticresource acred}" /> </grid.resources> <tabcontrol style="{staticresource lefttabcontrol}" background="#fafafafa" horizontalalignment="stretch"> <tabitem x:name="connectionlabeltab" style="{staticresource tab2}" focusable="false"> <tabitem.headertemplate> <datatemplate> <border x:name="connectionlabelborder" background="{binding labelcolor, mode=twoway, updatesourcetrigger=propertychanged}" width="70" datacontext="{binding relativesource={relativesource ancestortype=usercontrol}, path=datacontext}"> <textblock x:name="connectionlabeltext" text="{binding labeltext, mode=twoway, updatesourcetrigger=propertychanged}" padding="0,4,0,4" foreground="#fafafafa" horizontalalignment="center" verticalalignment="center" fontsize="10"/> </border> </datatemplate> </tabitem.headertemplate> </tabitem> </tabcontrol> </grid> xaml.cs
public partial class tabpanel : usercontrol, inotifypropertychanged { brush labelcolor; string labeltext; public tabpanel() { initializecomponent(); labelcolor = bggrid.resources["redbrush"] brush; labeltext = "disconnected"; } public brush labelcolor { { return labelcolor; } set { labelcolor = value; propertychanged(this, new propertychangedeventargs("labelcolor")); } } public event propertychangedeventhandler propertychanged; public void setconnected() { labelcolor = bggrid.resources["greenbrush"] brush; labeltext = "connected"; } } is there better/proper way change background color dynamically? how can fix code stop getting system.arguementexception? if comment out setting labelcolor code works fine , text changes expected.
ill take stab @ not sure if work may need invoke on dispatcher.
public void setconnected() { dispatcher.begininvoke(new action(() => { labelcolor = bggrid.resources["greenbrush"] brush; labeltext = "connected"; }); } the reason being, if setconnected method called somewhere off of ui thread (like timer or not directly called ui), need "reintegrate" ui thread in order update of ui properties. common thing when dealing wpf binding.
Comments
Post a Comment