Default ValueConverter for Binding

Is there a way to get WPF to automatically apply the Converter binding to all bindings of a certain type?

I saw this question , but it covers a different case (localization) and therefore does not have satisfactory answers.

My problem: I have model classes containing Commands that I would like to bind to WPF commands. Since model classes are not instrumental, I cannot implement WPF ICommand . Instead, I have a CommandConverter that wraps CommandModel in WPF ICommand s:

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

This works very well, except that it is easy to forget about Converter= , and WPF does not give any indication that the binding failed.

Now my question is: is it possible to force WPF to always apply the converter to certain types of bindings? Or, alternatively, how can I get WPF to give me the correct errors when command binding fails?

+4
source share
3 answers

I don’t think that you can without any subclassification of Button (you probably do not want to do this), or define your own nested property and use the TypeConverter attribute on it.

If you want to use the default converter using the TypeConverter attribute in the new attached property, you can see Rob Rel's informative post here or MSDN here .

+2
source

So far I have never done this, could a custom markup extension be defined? This should cause the value to be sent to your class that implements the markup extension, and then from there you can return ICommand , which expects the Command property.

As I said, I never created a single self, but Google Search seems to provide a few articles on how to do this.

+2
source

Check the debug output window. Usually you see binding errors.

0
source

Source: https://habr.com/ru/post/1286129/


All Articles