Bindable Converter Parameter

Introduction

Binding is one of the most powerful features introduced in WPF, it allows us to implement complex data-based UI, using minimum of declarative code (XAML), while relying on a 'built-in', out of the box mechanism that connects data to UI-controls in a direct way or through Converters.

Binding converters are by nature an encapsulated functions that receive the bound value, manipulate it in a specific manner, and return the manipulated result. On many occasions another source of information is needed for the converter in order to 'do its job', this source is called 'ConverterParameter' and it's one of the converter's Convert and ConvertBack method parameters.

WPF's out-of-the-box Bindings restricts the ConverterParameter to receive (from XAML) only Static or Resource values, while passing it a Binding expression is forbidden:

//
// this line of xaml produce the following error:
// <TextBox Text="{Binding Path=FirstName,Converter={StaticResource TestValueConverter},ConverterParameter={Binding Path=ConcatSign}}" />
A 'Binding' cannot be set on the 'ConverterParameter' property of type 'Binding'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.

In many cases this feature (Bindable ConverterParameter) is exactly what ones need. I.e., the ConverterParameter value which is a Binding-Expression (rather than a static predefined value) will be evaluated each time the binding evaluation is triggered, and will passed to the Converter as the ConverterParameter parameter.

In this article I'll show a relatively simple and straightforward technique to achieve just that. The XAML part that will implement the bound ConverterParameter will look as close as possible to the Error producing line of code above, with the exception that it will work... 

Bindable Converter Parameter Take-3  

 

相关连接

 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28623727/viewspace-1149661/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/28623727/viewspace-1149661/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值