Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind a Converter defined in the code behind

As part of the solution refactoring I had to move the Converter in to a Dependency Injected UI service. Now my ValueConverter is present inside an Instance. Earlier I had defined a local resource and mentioned it for the Converter which worked like a charm. After refactoring as mentioned, I tried my luck in Binding to Converter and it just said Binding cannot be done to Converter. Reading this http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/d6a95f05-4338-44a4-a834-bbfe71e893ac/ found its by design.

Can you please tell me how to specify my Converter in xaml which is present inside an instance in the code behind (say UIHelperService.FormatConverter in the Views code behind).

like image 415
ioWint Avatar asked Apr 24 '26 03:04

ioWint


1 Answers

If the converter can be shared between instances, make it a static member somewhere and then....

{Binding Whatever Converter={x:Static src:MyClass.MyConverter}}

If it's an instance member, you may need to manually set up the binding in code-behind... but converters should almost never be instance-specific. Instead, you can use ConverterParameters to achieve this behavior if you need it.

like image 112
Robert Fraser Avatar answered Apr 27 '26 16:04

Robert Fraser



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!