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:
//
<TextBoxText="{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  


相關連接



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章