使用WPF中值转换器,而无需他们先定义为资源 [英] Using Value Converters in WPF without having to define them as resources first

查看:600
本文介绍了使用WPF中值转换器,而无需他们先定义为资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时有可能使用值转换器,而无需事先将它们定义为资源?

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">



那岂不是可能的,而不必申报trivialFormatter资源在Window.Resources,我可以直接从按钮的宽度结合参考呢?类似

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 IValueConverters (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中值转换器,而无需他们先定义为资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆