92

How do you bind to an objects method in this scenario in WPF?

public class RootObject
{
    public string Name { get; }

    public ObservableCollection<ChildObject> GetChildren() {...}
}

public class ChildObject
{
    public string Name { get; }
}

XAML:

<TreeView ItemsSource="some list of RootObjects">
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type data:RootObject}" 
                                  ItemsSource="???">
            <TextBlock Text="{Binding Path=Name}" />
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="{x:Type data:ChildObject}">
            <TextBlock Text="{Binding Path=Name}" />
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>

Here I want to bind to the GetChildren method on each RootObject of the tree.

EDIT Binding to an ObjectDataProvider doesn't seem to work because I'm binding to a list of items, and the ObjectDataProvider needs either a static method, or it creates it's own instance and uses that.

For example, using Matt's answer I get:

System.Windows.Data Error: 33 : ObjectDataProvider cannot create object; Type='RootObject'; Error='Wrong parameters for constructor.'

System.Windows.Data Error: 34 : ObjectDataProvider: Failure trying to invoke method on type; Method='GetChildren'; Type='RootObject'; Error='The specified member cannot be invoked on target.' TargetException:'System.Reflection.TargetException: Non-static method requires a target.

Community
  • 1
  • 1
Cameron MacFarland
  • 70,676
  • 20
  • 104
  • 133
  • Yeah, you're right. ObjectDataProvider does have an ObjectInstance property (to call its method on a specific instance) but I don't think it's a dependency property, so you can't bind it (AFAIK). – Matt Hamilton Feb 02 '09 at 06:43
  • 1
    Yeah I tried to bind to ObjectInstance and found out it's not a dependency property. – Cameron MacFarland Feb 02 '09 at 07:28
  • I'll leave my answer there anyway, both to give your update some context and to help someone else who finds this question with a similar enough problem. – Matt Hamilton Feb 02 '09 at 08:43
  • Do you actually need to bind to ObjectInstance? (Will it change) Assuming so you could instead create your own change-event handling and update the ObjectDataProvider in code... – Tim Lovell-Smith Jan 18 '10 at 15:56
  • 1
    Just updated my answer with some source code, a year after the fact. – Drew Noakes Jun 06 '10 at 02:40

8 Answers8

73

Another approach that might work for you is to create a custom IValueConverter that takes a method name as a parameter, so that it would be used like this:

ItemsSource="{Binding 
    Converter={StaticResource MethodToValueConverter},
    ConverterParameter='GetChildren'}"

This converter would find and invoke the method using reflection. This requires the method to not have any arguments.

Here's an example of such a converter's source:

public sealed class MethodToValueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var methodName = parameter as string;
        if (value==null || methodName==null)
            return value;
        var methodInfo = value.GetType().GetMethod(methodName, new Type[0]);
        if (methodInfo==null)
            return value;
        return methodInfo.Invoke(value, new object[0]);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException("MethodToValueConverter can only be used for one way conversion.");
    }
}

And a corresponding unit test:

[Test]
public void Convert()
{
    var converter = new MethodToValueConverter();
    Assert.AreEqual("1234", converter.Convert(1234, typeof(string), "ToString", null));
    Assert.AreEqual("ABCD", converter.Convert(" ABCD ", typeof(string), "Trim", null));

    Assert.IsNull(converter.Convert(null, typeof(string), "ToString", null));

    Assert.AreEqual("Pineapple", converter.Convert("Pineapple", typeof(string), "InvalidMethodName", null));
}

Note that this converter does not enforce the targetType parameter.

Michael Myers
  • 188,989
  • 46
  • 291
  • 292
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
27

Not sure how well it will work in your scenario, but you can use the MethodName property on ObjectDataProvider to have it call a specific method (with specific parameters of you MethodParameters property) to retrieve its data.

Here's a snippet taken directly from the MSDN page:

<Window.Resources>
    <ObjectDataProvider ObjectType="{x:Type local:TemperatureScale}"
        MethodName="ConvertTemp" x:Key="convertTemp">
        <ObjectDataProvider.MethodParameters>
            <system:Double>0</system:Double>
            <local:TempType>Celsius</local:TempType>
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
</Window.Resources>

So that's an ObjectDataProvider that's calling a ConvertTemp method on an instance of a TemperatureScale class, passing two parameters (0 and TempType.Celsius).

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
Matt Hamilton
  • 200,371
  • 61
  • 386
  • 320
13

Do you have to bind to the method?

Can you bind to a property who's getter is the method?

public ObservableCollection<ChildObject> Children
{
   get
   {
      return GetChildren();
   }
}
Michael Prewecki
  • 2,064
  • 4
  • 16
  • 28
  • 3
    I take Cameron's comment to mean that he is binding to a type that he can not add a property to. – Drew Noakes Nov 05 '09 at 10:21
  • 2
    You should avoid binding to properties that call methods esp if the method could potentially be long running. Having such methods it properties is not good design as a consumer of code expects a property to only access a local variable. – markmnl Dec 15 '10 at 16:52
  • 1
    @markmnl so what is the point of binding directly to function? So OP's question does not make any sense on your case? – Teoman shipahi Feb 14 '15 at 22:18
6

Unless you can add a property to call the method (or create a wrapper class that adds that property) the only way I know of is using a ValueConverter.

Nir
  • 29,306
  • 10
  • 67
  • 103
3

ObjectDataProvider also has an ObjectInstance property that can be used instead of ObjectType

Graham Ambrose
  • 1,891
  • 2
  • 13
  • 17
3

You can use System.ComponentModel to define properties for a type dynamically (they're not part of the compiled metadata). I used this approach in WPF to enable binding to a type that stored its values in fields, as binding to fields is not possible.

The ICustomTypeDescriptor and TypeDescriptionProvider types might allow you to achieve what you want. According to this article:

TypeDescriptionProvider allows you to write a separate class that implements ICustomTypeDescriptor and then to register this class as the provider of descriptions for other types.

I haven't tried this approach myself, but I hope it's helpful in your case.

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
1

To bind to an object's method in your WPF scenario, you can bind to a property that returns a delegate.

WonderWorker
  • 8,539
  • 4
  • 63
  • 74
Austin_Anderson
  • 900
  • 6
  • 16
0

Same as Drew Noakes's answer, but with an ability to use extension methods.

public sealed class MethodToValueConverter : IValueConverter
{
    public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        var methodName = parameter as string;
        if (value == null || methodName == null)
            return value;
        var methodInfo = value.GetType().GetMethod(methodName, Type.EmptyTypes);
        if (methodInfo == null)
        {
            methodInfo = GetExtensionMethod(value.GetType(), methodName);
            if (methodInfo == null) return value;
            return methodInfo.Invoke(null, new[] { value });
        }
        return methodInfo.Invoke(value, Array.Empty<object>());
    }

    static MethodInfo? GetExtensionMethod(Type extendedType, string methodName)
    {
        var method = Assembly.GetExecutingAssembly()
            .GetTypes()
            .Where(type => !type.IsGenericType && !type.IsNested)
            .SelectMany(type => type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic), (_, method) => method)
            .Where(m => m.IsDefined(typeof(ExtensionAttribute), false))
            .Where(m => m.GetParameters()[0].ParameterType == extendedType)
            .FirstOrDefault(m => m.Name == methodName);
        return method;
    }

    public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        throw new NotSupportedException("MethodToValueConverter can only be used for one way conversion.");
    }
}
Andrew KeepCoding
  • 7,040
  • 2
  • 14
  • 21