如何在 Silverlight XAML 中引用另一个命名空间中的绑定转换器?
由于您显然无法在 C# 中创建 Silverlight DataTemplate,因此我尝试在 XAML 中创建一个。我有一个需要引用的转换器,我已在另一个命名空间中用 C# 定义了该转换器。我尝试这样做:
<UserControl.Resources>
<DataTemplate x:Key="PriceTemplate">
<TextBlock Text="{Binding Price, Converter={Converters:PriceConverter}}" />
</DataTemplate>
</UserControl.Resources>
其中 Converters 是指向正确名称空间的 xmlns。但是,我收到一个编译错误:
类型“转换器:PriceConverter”是 像标记扩展一样使用,但确实 不是从 MarkupExtension 派生的。
我尝试将 System.Windows.Markup.MarkupExtension 作为父级添加到我的转换器中,但它显然在 Silverlight 中不存在。
如何在 XAML 中引用我的转换器,而不必在 XAML 中重写它?
Since you apparently can't create a Silverlight DataTemplate in C#, I'm trying to create one in XAML. I have a converter that I need to refer to, that I have defined in C# in another namespace. I've tried doing this:
<UserControl.Resources>
<DataTemplate x:Key="PriceTemplate">
<TextBlock Text="{Binding Price, Converter={Converters:PriceConverter}}" />
</DataTemplate>
</UserControl.Resources>
Where Converters is an xmlns that points to the correct namespace. However, I get a compilation error that says:
Type 'Converters:PriceConverter' is
used like a markup extension but does
not derive from MarkupExtension.
I tried adding System.Windows.Markup.MarkupExtension as a parent to my converter, but it apparently doesn't exist in Silverlight.
How can I refer to my converter in XAML, without having to rewrite it in XAML?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您想先创建一个静态资源,然后绑定到作为静态资源的转换器。
因此,“conv:”xml 命名空间在文档顶部注册,就像对自定义控件所做的那样:
此示例改编自以下链接的教程,该教程涉及 WPF 的同一问题
: dev102.com/2008/07/17/wpf-binding-converter-best-practices/" rel="noreferrer">http://www.dev102.com/2008/07/17/wpf-binding-converter-best -实践/
You want to make a static resource first, then bind to the converter that is a static resource.
So the "conv:" xml namespace was registered at the top of the document like you do with custom controls:
This example is adapted from the below linked tutorial regarding the same issue for WPF:
http://www.dev102.com/2008/07/17/wpf-binding-converter-best-practices/
您似乎将类型与实例混淆了。转换器类型将存在于命名空间“中”,但是在绑定中我们不指定类型作为转换器。相反,我们为绑定提供该类型的实际实例。
通常,
IValueConverter
实例是无状态的,因此我们可以在加载 DataTemplate 实例的可用资源字典链中的任何位置保存一个公共实例。在 xaml 中,我们可以通过创建新别名来覆盖另一个名称空间来引用它。考虑到这一点,您的 xaml 可能看起来像这样:-
You seem to be confusing Types with Instances. A converter type will exist "in" a namespace however in binding we do not specify a type as the converter. Instead we give the binding an actual instance of that type.
Generally
IValueConverter
instances are stateless, hence we can hold a common instance anywhere in a the chain of resource dictionaries available where the instance of a DataTemplate is loaded.In xaml we can reference another namespace by creating a new alias to cover it. With that in mind your xaml could look something like this:-
添加到@Rokk 发布的答案。
Adding to the answer posted by @Rokk.
除了建议在资源字典中创建 ValueConverter 实例的其他答案之外,另一个解决方案是通过 MarkupExtention 实现它:
使用此解决方案,您可以直接引用转换器(本质上是 MarkupExtension),而无需定义资源。
In addition to the other answers suggesting creating an instance of the ValueConverter in a resource dictionary, another solution is to implement it via a MarkupExtention:
With this solution you can directly reference the converter (which is in essence a MarkupExtension) whithout defining a resource.