问题描述
是否可以使用价值转换器而不必事先将其定义为资源?
Is it possible to use value converters without having to define them beforehand as resources?
现在我有
<Window.Resources>
<local:TrivialFormatter x:Key="trivialFormatter" />
</Window.Resources>
和
<Button Width="{Binding Width, ElementName=textBox1, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource trivialFormatter}}" Height="50">
不可能,而不是在Window.Resources中声明trivialFormatter资源,我可以直接从Button的宽度绑定中引用它?像
Wouldn't it be possible that instead of having to declare the trivialFormatter resource in Window.Resources, I could directly refer it from the Button's width binding? Something like
Converter = {local:TrivialFormatter}
谢谢
推荐答案
在单例型 IValueConverter
s(例如,他们不需要任何来自当前绑定实例的状态)我使用静态转换器,即:
In the case of singleton-type IValueConverter
s (e.g. they don't need any state from the current binding instance) I use static converters, i.e.:
Converter={x:Static SomeNamespace:SomeConverter.Instance}
还有一个WPF博士发表了伟大的发言,使用标记扩展名使其更清晰的内嵌。
There's also a great post by Dr. WPF on using a markup extension to make it cleaner inline here.
这篇关于在WPF中使用Value Converters,而不必首先将它们定义为资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!