c# - StoryBoard object becomes read-only when set by a Style -


i have attached behavior has single attached property of type storyboard. want set property on every item in listview. xaml looks this:

<grid>    <grid.resources>       <storyboard x:key="theanimation" x:shared="false">          <doubleanimation from="0.0" to="1.0" duration="0:0:0.20"              storyboard.targetproperty="opacity" />       </storyboard>    </grid.resources>     <listview>       <listview.resources>          <style targettype="{x:type listviewitem}">             <setter property="local:mybehavior.animation"                value="{staticresource theanimation}" />          </style>       </listview.resources>    </listview> </grid> 

so far good. code in 'mybehavior' tries this:

private static void animationchanged(dependencyobject d,    dependencypropertychangedeventargs e) {    var listviewitem = d listviewitem;    if (d == null)       return;     var sb = e.newvalue storyboard;    if (sb == null)       return;     storyboard.settarget(sb, listviewitem);    sb.begin(); } 

but invalidoperationexception thrown on call storyboard.settarget(): "cannot set property on object 'system.windows.media.animation.storyboard' because in read-only state." if inspect storyboard in debugger, can see both issealed , isfrozen properties set true.

by contrast, if set mybehavior.animation directly on listview don't need use style, storyboard arrives unsealed , able set target , run successfully. that's not want it.

why storyboard being sealed, , there can prevent this?

update: can solve problem adding right after null check:

if(sb.issealed)    sb = sb.clone(); 

but i'm still curious what's going on. apparently somewhere (style? setter?) freezing/sealing object in setter.value.

i'm far expert in wpf, cannot explain finer details of why choice microsoft made. understand it, main issue object declared resource shared multiple other objects. such, prevented modifying it.

if still want go resources route, possible can treat resource {dynamicresource...} instead of {staticresource...} , might allow modify object that's been used other object. said, i'm not expert in wpf , admit still being bit cloudy on different between dynamicresource , staticresource, have vague recollection addresses scenario. :)


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -