Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding to a method in DataContext
    primarykey
    data
    text
    <p>Is it possible to use a method of the DataContext in a binding?</p> <p>E.g. the signature <code>public bool ProjectIsActive(int number)</code> [note: not static], can it be declared in a binding?</p> <p>EDIT: Following the suggestion of ywm and Sheridan I tried adding a resource to my Window with name _this.<br> I changed the signature to <code>public bool ProjectIsActive(object number)</code>.</p> <pre><code>&lt;Window.Resources&gt; &lt;ObjectDataProvider x:Key="ProjectIsActive" ObjectInstance="_this.DataContext" MethodName="ProjectIsActive"&gt; &lt;ObjectDataProvider.MethodParameters&gt; &lt;sys:Object&gt;&lt;/sys:Object&gt; &lt;/ObjectDataProvider.MethodParameters&gt; &lt;/ObjectDataProvider&gt; &lt;/Window.Resources&gt; </code></pre> <p>Then the Binding of the Trigger is set as:</p> <pre><code>&lt;Binding Source="{StaticResource ProjectIsActive}" Path="MethodParameters[0]" BindsDirectlyToSource="true" /&gt; </code></pre> <p>It gives the error: <em>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&amp; e)'</em></p> <p>The method signature of ObjectDataProvider.MethodParameters doesn't distinghuish return value from input paramter, I don't know how to proceed.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload