c# - Animating BorderBrush from DynamicResource animates everything using that brush -



c# - Animating BorderBrush from DynamicResource animates everything using that brush -

i trying color border based on state, , event. have on, , off state, "poked" event. on = green, off = red, when poked want fade bluish original color (based on state). in application resource, , used dynamicresource.

i have code that, whenever trigger poked event, using color object set to, changes bluish color. suspect happening color transition running on color stored in application resource, rather color of object in particular.

i may way off mark how i'm handling animation. appreciate help.

mainwindow.xaml

<window x:class="coloranimatorissue.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:coloranimatorissue" title="mainwindow" height="350" width="525" datacontext="{binding relativesource={relativesource self}}"> <window.resources> <local:statecolorconverter x:key="colorconverter"/> <storyboard x:key="poke1"> <coloranimation storyboard.targetname="b1" storyboard.targetproperty="(borderbrush).(solidcolorbrush.color)" from="{dynamicresource pokecolor}" duration="0:0:2"/> </storyboard> <storyboard x:key="poke2"> <coloranimation storyboard.targetname="b2" storyboard.targetproperty="borderbrush.color" from="{dynamicresource pokecolor}" duration="0:0:2"/> </storyboard> </window.resources> <grid> <button content="poke 1" horizontalalignment="left" verticalalignment="top" width="75" margin="7,37,0,0" click="poke1_click"/> <button content="poke 2" horizontalalignment="left" verticalalignment="top" width="75" margin="87,37,0,0" click="poke2_click"/> <border x:name="b1" borderbrush="{binding mystate1, converter={staticresource colorconverter}}" borderthickness="10" margin="199,37,217,183"> <rectangle height="100" width="100" fill="black"/> </border> <border x:name="b2" borderbrush="{binding mystate2, converter={staticresource colorconverter}}" borderthickness="10" margin="305,37,111,183"> <rectangle height="100" width="100" fill="black"/> </border> <button content="toggle 1" horizontalalignment="left" verticalalignment="top" width="75" margin="7,10,0,0" click="toggle1_click" /> <button content="toggle 2" horizontalalignment="left" verticalalignment="top" width="75" margin="87,10,0,0" click="toggle2_click" /> </grid>

code behind

public enum states { on, off } /// <summary> /// interaction logic mainwindow.xaml /// </summary> public partial class mainwindow : window, inotifypropertychanged { states mystate1 = states.off; states mystate2 = states.off; public mainwindow() { initializecomponent(); } public event propertychangedeventhandler propertychanged; public states mystate1 { { homecoming mystate1; } set { if(mystate1 != value) { mystate1 = value; onpropertychanged("mystate1"); } } } public states mystate2 { { homecoming mystate2; } set { if (mystate2 != value) { mystate2 = value; onpropertychanged("mystate2"); } } } void onpropertychanged(string propertyname) { if(propertychanged != null) { propertychanged(this, new propertychangedeventargs(propertyname)); } } private void poke1_click(object sender, routedeventargs e) { (findresource("poke1") storyboard).begin(this); } private void poke2_click(object sender, routedeventargs e) { (findresource("poke2") storyboard).begin(this); } private void toggle1_click(object sender, routedeventargs e) { mystate1 = (mystate1 == states.on) ? states.off : states.on; } private void toggle2_click(object sender, routedeventargs e) { mystate2 = (mystate2 == states.on) ? states.off : states.on; } }

state color converter

public class statecolorconverter : ivalueconverter { public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { switch((states)value) { case states.on: homecoming application.current.findresource("onbrush") brush; case states.off: homecoming application.current.findresource("offbrush") brush; default: homecoming null; } } public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { throw new notimplementedexception(); } }

app resources

<application.resources> <color a="255" r="0" g="255" b="0" x:key="oncolor"/> <solidcolorbrush color="{dynamicresource oncolor}" x:key="onbrush"/> <color a="255" r="255" g="0" b="0" x:key="offcolor"/> <solidcolorbrush color="{dynamicresource offcolor}" x:key="offbrush"/> <color a="255" r="0" g="0" b="255" x:key="pokecolor"/> </application.resources>

try setting x:shared="false" on brush resources forcefulness separate instance created each time referenced.

c# wpf animation

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

java - Parsing XML, skip certain tags -

c# - ASP.NET MVC Sequence contains no matching element -