Silverlight:如何在样式的 setter 中使用绑定(或等效的解决方法)
如果回答此问题的人是正确的,则您不能将绑定作为Silverlight 中样式的 setter 中的值。这很遗憾,因为我有 4 个文本块,它们的不透明度属性都使用完全相同的绑定。无论如何,在某种意义上是否可以“设置”其不透明度属性的样式,以便所有四个都指向相同的绑定?否则,我必须单独设置每个不透明度属性。就我而言,情况更糟 - 所有四个都共享其他属性绑定,这意味着每个 TextBlock 声明都非常长,但它们实际上都是相同的(即它们的属性绑定)。我知道我可以在代码隐藏中简洁地设置它们的所有共享属性绑定,但我想要一个 XAML 解决方案(如果有)。
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是它是如何完成的。您使用
ContentControl
并为其指定一个ControlTemplate
作为静态资源:-现在您可以根据需要将其他属性绑定到控件模板中。
这种方法可以扩展到
Style
:-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
:-在此 博客文章 以及样式设置器中对
Binding
的支持是 宣布用于 SL5。Check out
SetterValueBindingHelper
in this blog article and support forBinding
in style setters is announced for SL5.在 Silverlight 中:嗯...是的,你不能进行绑定。这里我使用了静态资源(这可能无法满足您的需求)。这是您无需在代码中进行绑定即可获得的最接近结果。
编辑:
好吧,无论如何,这是在 WPF 中...
在这里,在 WPF 中:
当然,您可以获得比这更多的创意。另外,根据定义样式的方式/时间/地点,有时在代码中完成它会更容易。
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.
几天前我遇到了完全相同的问题,并发现了以下博客文章。
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
它就像一个魅力。
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.