Visual State on Initialize (Chicken vs. Egg)
even though i've been working silverlight time, inner workings of visual state manager still mystify me. trying create control in code , set control's visual state. let's have custom control called selectorcontrol inherits contentcontrol. selectorcontrol has dependencyproperty called isselected. when isselected set true, changes visual state "selected" , on false "unselected" in dependencypropertychanged callback, so...
private shared sub onisselectedchanged(byval o as dependencyobject, byval e as dependencypropertychangedeventargs) directcast(o, selectorcontentcontrol).notifypropertychanged("isselected") directcast(o, selectorcontentcontrol).onisselectedchanged(e) end sub private sub onisselectedchanged(byval e as dependencypropertychangedeventargs) if directcast(e.newvalue, boolean) then me.gotostate("selected") else me.gotostate("unselected") end if end sub
the gotostate sub calls visualstatemanager.gotostate sub:
public sub gotostate(byval name as string) if not visualstatemanager.gotostate(me, name, true) then me.dispatcher.begininvoke(new action(of control, string, boolean)(addressof visualstatemanager.gotostate), me, name, true) end if end sub
selectorcontrol being used inside various itemscontrol-derived controls have designed, such when selectorcontrol created, checks sort of selected items list see if isselected should set true or false. actual itemscontrols long post here, use virtualization such each selectorcontrol created on demand during measureoverride call. so, think problem selectorcontrol created , isselected set during measureoverride of parent, selectorcontrol has not yet been added visualtree, visual state change never occurs.
so, question is, there ideas of reliable way of setting visual state on initialization? thought routing failed visualstatemanager.gotostate function calls through dispatcher (see above gotostate sub) able solve problem, not seem work. other solution work selectorcontrol check isselected property during loaded event , set visual state explicitly, seems superfluous , less elegant had hoped. hoping there sort of way "queue-up" visual state change honored when control added tree. (other creating queue within control myself -- work -- kind of clunky again.)
any thoughts appreciated!
why reluctance use loaded event?
that's it's for. ther things controls not ready until initialized , "loaded" visual tree, , event way it.
if don't explicitly setting or using on code-behind, use behavior instead triggered loaded.
have looked @ expression blend's "activatestate" action? bind control, , set target event type, , set state when fired. sounds looking for. event if don't have blend, can download blend sdk free:
Silverlight > Silverlight Controls and Silverlight Toolkit
Comments
Post a Comment