views:

665

answers:

3

Is there a way to get WPF to automatically apply a Converter to all bindings of a specific type?

I've seen this question, but it covers a different case (localisation) and thus has no satisfying answers.

My problem: I've got model classes containing Commands, which I would like to bind to WPF Commands. Since the model classes are toolkit-independent, I cannot implement WPF's ICommand there. Instead I have a CommandConverter which wraps CommandModels into WPF ICommands:

<Button Command="{Binding MyCommand, Converter={StaticResource CommandConverter}}" />

This works quite well, except that it is easy to forget about the Converter= and WPF doesn't give any indication that the binding failed.

My question is now: Is there a possibility to force WPF to always a apply a converter to specific types of bindings? Or, alternatively, how can I get WPF to give me proper errors when a Command binding fails?

A: 

Check the debug output window. Normally you get to see the binding errors there.

Vasu Balakrishnan
No binding errors there.
David Schmitt
+2  A: 

I don't think you can without either sub-classing Button (you probably don't want to do this), or defining your own attached property and using a TypeConverter attribute on it.

If you want to go with using a default converter via the TypeConverter attribute on a new attached property, you can look at Rob Relyea's informative post here, or MSDN here.

micahtan
From the first post it looks as if there is no need for the property to be an *attached* property. Anyways, I'll try it out and report back!
David Schmitt
Actually, TypeConverters only seem to be used to convert from (XAML-)strings to actual property values. Bummer.
David Schmitt
+1  A: 

While I've never done it, would it be possible to define a custom Markup Extension? That should cause the value to be sent to your class that implements the Markup Extension, and then from there you can return an ICommand that the Command property is expecting.

As I said, I've never created one my self, but a Google Search seems to bring up a few articles on how to do it.

Andy
Interesting idea. Though that only moves the problem from having to remember to use the converter to having to remember to use the custom markup extension.
David Schmitt
True, but hopefully the syntax for the markup extension would be smaller than what you posted above - a small gain, if nothing else.
Andy