Well, I didnโt want to have a bunch of ICommands in MVMM ViewModels, so I decided to create MarkupExtension for WPF so that you pass it a string (method name) and it returns you ICommand, which executes the method.
here's a snippet:
public class MethodCall : MarkupExtension { public MethodCall(string methodName) { MethodName = methodName; CanExecute = "Can" + methodName; } public override object ProvideValue(IServiceProvider serviceProvider) { Binding bin= new Binding { Converter = new MethodConverter(MethodName,CanExecute) }; return bin.ProvideValue(serviceProvider); } } public class MethodConverter : IValueConverter { string MethodName; public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
It works great, except when the binding does not work (for example, you are mistaken).
When you do this in xaml: {Binding MyPropertyName} you see in the output window whenever the failure fails. and it tells you the property nameName, etc.
The MethodConverter class can tell you the name of the method that failed, but it cannot specify the type of the source object. Since the value will be zero.
I can't figure out how to save the type of the source object, so for the next class
public class MyClass { public void MyMethod() { } }
and the following haml:
<Button Command={d:MethodCall MyMethod}>My Method</Button>
He currently says:
"Could not bind to method 'MyMethod' on object
but I would like him to say:
"Could not bind to method 'MyMethod' on object MyClass
Any ideas?
source share