是否可以在 ControlTemplate 中包含 Xaml 资源?
我正在开发一个 Silverlight 模板化控件(即它派生自 Control 并在 generic.xaml 中将其外观实现为 ControlTemplate),并且在我的绑定之一中遇到了对 ValueConverter 的需求。通常,我只是在使用的控件的资源中创建 ValueConverter 的实例,但我不知道如何使用 ControlTemplate 来做到这一点。如果可能的话,我希望将此实现全部保留在 Xaml 中,但我想我可以重写 OnApplyTemplate 并手动创建与转换器的绑定。
I'm working on a Silverlight templated control (i.e. it derives from Control and has its look implemented as a ControlTemplate in generic.xaml) and came across the need for a ValueConverter in one of my bindings. Normally I'd just create an instance of the ValueConverter in the Resources of the control working with but I can't figure out how to do that with a ControlTemplate. I'd like to keep this implementation all in Xaml if possible, but I suppose I could override OnApplyTemplate and manually create the binding with a converter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这在 Silverlight 3 中确实有效。您只需在控件模板中添加一个资源部分,并将 ValueConverter 添加到模板的资源中:
但是,ValueConverter 在控件模板中经常被误用。您可能想改用 TypeConverter。 Bryant Likes 写了一篇很好的文章,讨论 此场景中 ValueConverter 和 TypeConverter 之间的区别。
This does work in Silverlight 3. You just need to add a resources section inside your Control Template, and add the ValueConverter to the template's resources:
However, ValueConverters are often misused in Control Templates. You may want to use a TypeConverter instead. Bryant Likes wrote a good article discussing the difference between ValueConverter and TypeConverter in this scenario.