wpf - Binding a resource to a custom control property -


i creating custom button shows faded text normally, , full-strength text on mouseover or mousedown. have defined 2 resources in generic.xaml of control represent brushes these text colors:

<!-- text brushes --> <solidcolorbrush x:key="normaltextbrush" color="black" /> <solidcolorbrush x:key="fadedtextbrush" color="gray" /> 

the control compiles , works fine in configuration.

but want let control user set text color, using custom control's foreground property. so, changed resource declarations this:

<!-- text brushes --> <solidcolorbrush x:key="normaltextbrush" color="{binding path=foreground, relativesource={relativesource templatedparent}}" /> <solidcolorbrush x:key="fadedtextbrush" color="{binding path=foreground, relativesource={relativesource templatedparent}, converter={staticresource colorconverter}, converterparameter='1.2'}" /> 

the second declaration uses hsl value converter fade text color.

now control doesn't work, , following error in output window:

system.windows.data error: 2 : cannot find governing frameworkelement or frameworkcontentelement target element. bindingexpression:path=foreground; dataitem='taskbutton' (name='button1'); target element 'solidcolorbrush' (hashcode=38118303); target property 'color' (type 'color') system.windows.data error: 2 : cannot find governing frameworkelement or frameworkcontentelement target element. bindingexpression:path=foreground; dataitem=null; target element 'solidcolorbrush' (hashcode=47449297); target property 'color' (type 'color') 

i'm not sure data error telling me. can tell me what's going on , how fix it? help.

relativesource templatedparent (iirc) has meaning within control template, , refers property on instance of control on template applied.

a usercontrol's content not template of usercontrol. binding won't consider parent usercontrol viable target.

the error message refers fact solidcolorbrush not have template; not extend system.windows.controls.control, base type of (most) templated ui controls. see control.template more information templating.

what want set relative source of findancestor.

{binding foreground, relativesource={relativesource findancestor, ancestortype=usercontrol}} 

this walk visual (or logical?) tree find first ancestor of type usercontrol, bind against public property called foreground.

however not work if solidcolorbrush defined resource. resources not part of visual (or logical tree, or both? still not clear) , therefore relativesource binding not able walk tree's ancestry.

you have use binding directly on whatever control wish have same foreground color usercontrol.


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -