resources - WPF StaticResource vs DynamicResource -
i have xaml below
<window.resources> <solidcolorbrush x:key="brushme" color="red"/> <solidcolorbrush x:key="freezebrushme" po:freeze="true" color="yellow"/> </window.resources> <stackpanel> <button background="{staticresource brushme}">static</button> <button background="{dynamicresource brushme}">dynamic</button> <button click="button_click">changecolorproperty</button> <button click="button1_click">changebrushobject</button> <!--<button background="{staticresource notexistresource}" click="button_click_1">generateresourceforstatic</button>--> <button background="{dynamicresource notexistresource}" click="button_click_1">generateresourcefordynamic</button> <button background="green" click="button_click_1">fixedbrush</button> <button background="{staticresource freezebrushme}">freezebrushstatic</button> <button background="{dynamicresource freezebrushme}">freezebrushdynamic</button> <button click="button_click_2">changefreezecolorproperty</button> <button click="button_click_3">changefreezebrushobject</button> </stackpanel>
in code behind, looks like
private void button_click(object sender, routedeventargs e) { solidcolorbrush brush = (solidcolorbrush)this.findresource("brushme"); brush.color = colors.blue; this.resources["brushme"] = brush; } private void button1_click(object sender, eventargs e) { solidcolorbrush newbrush = new solidcolorbrush(colors.yellow); this.resources["brushme"] = newbrush; } private void button_click_1(object sender, routedeventargs e) { this.resources.add("notexistresource", new solidcolorbrush(colors.green)); } private void button_click_2(object sender, routedeventargs e) { solidcolorbrush brush = (solidcolorbrush)this.findresource("freezebrushme"); brush.color = colors.blue; this.resources["freezebrushme"] = brush; } private void button_click_3(object sender, routedeventargs e) { solidcolorbrush newbrush = new solidcolorbrush(colors.red); this.resources["freezebrushme"] = newbrush; }
my question if click changebrushobject button first, change dynamic button yellow, click changecolorproperty button, change dynamic button blue, , not affecting static button.
however, if click changecolorproperty button on own, change both static , dynamic buttons blue.
why behaves this? wpf doesn't throw warnings when trying assign new object resource has been referenced static resource control?
i tested non-existing resource referenced dynamic resource. expected, if staticresource references non-existing resource, xaml complain @ compile time , dynamicresource not. however, if click generateresourcefordynamic button, shows green color background, fades away, , reappears again, , repeats. why happens this?
edit: added freeze resource, strange behaviour again. if click changefreezecolorproperty button, through exception. , if click changefreezebrushobject alone, change freezebrushdynamic button color. if click changefreezebrushobject , click changefreezecolorproperty, no exception thrown.
i think making resource freeze make properties of object linked resource readonly. why when changing property, through exception complaining readonly state. freeze doesn't stop changing object itself.
Comments
Post a Comment