If the person who answered this question is right, you cannot put a binding as the value in a setter in a style in Silverlight. Which is a shame, because I have 4 textblocks that all use the exact same binding for their Opacity property. Is there anyway to in a sense "style" their Opacity property so that all four of them point to the same binding? Otherwise, I have to set each Opacity property individually. In my case it's even worse - all four share other property bindings as well, which means each TextBlock declaration is pretty dang long, and yet they're all virtually the same (their property bindings, that is). I know I could concisely set all their shared property bindings in the code-behind, but I'd like a XAML solution if there is one.
Thanks!
Check out
SetterValueBindingHelper
in this blog article and support forBinding
in style setters is announced for SL5.I had the exact same problem a few days ago and found the following blog post. http://blogs.msdn.com/b/delay/archive/2009/11/02/as-the-platform-evolves-so-do-the-workarounds-better-settervaluebindinghelper-makes-silverlight-setters-better-er.aspx
It works like a charm.
Here is how its done. You use a
ContentControl
and specify aControlTemplate
for it as a static resource:-Now you can bung as may other properties with bindings in to the Control Template as you want.
This approach could be extended to
Style
:-In Silverlight: Well... yeah, you can't do a binding. Here I used a static resource, (which probably won't meet your needs). This is closest you are going to get without doing the bindings in code.
EDIT: Well, here it is in WPF anyway...
Here you go, in WPF:
Of course you can get a lot more creative than this. Also, depending on how / when / where your styles are defined, it is sometimes easier just to do it in code.