Is it possible to use a method of the DataContext in a binding?
E.g. the signature public bool ProjectIsActive(int number)
[note: not static], can it be declared in a binding?
EDIT: Following the suggestion of ywm and Sheridan I tried adding a resource to my Window with name _this.
I changed the signature to public bool ProjectIsActive(object number)
.
<Window.Resources>
<ObjectDataProvider x:Key="ProjectIsActive"
ObjectInstance="_this.DataContext"
MethodName="ProjectIsActive">
<ObjectDataProvider.MethodParameters>
<sys:Object></sys:Object>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
Then the Binding of the Trigger is set as:
<Binding Source="{StaticResource ProjectIsActive}" Path="MethodParameters[0]" BindsDirectlyToSource="true" />
It gives the error: System.Windows.Data Error: 35 : ObjectDataProvider: Failure trying to invoke method on type; Method='ProjectIsActive'; Type='String'; Error='No method was found with matching parameter signature.' MissingMethodException:'System.MissingMethodException: Method 'System.String.ProjectIsActive' not found. at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams) at System.Windows.Data.ObjectDataProvider.InvokeMethodOnInstance(Exception& e)'
The method signature of ObjectDataProvider.MethodParameters doesn't distinghuish return value from input paramter, I don't know how to proceed.
NOTE: The objective can be also achieved by using a MultiValueConverter and casting in the Convert method etc. I was only curious because binding to a property is so easy, and binding to a method [in my case] so difficult. There are also still better ways to avoid a call to a method.