mickeyt
mickeyt

Reputation: 416

WPF - binding to an explicitly implemented interface property from code behind/attached behaviour

I am trying to set up a binding to an explicitly implemented interface property from code-behind. The reason for the code-behind binding is that the path to the bound property can only be determined at run-time.

In the XAML, it is possible to bind thusly (example in MainWindow.xaml):

<TextBox Text="{Binding (local:IViewModel.Property)}"/>

and in fact, binding in the code behind works in a similar way (from MainWindow.xaml.cs):

var binding = new Binding("(local:IViewModel.Property)");

since WPF is able to pick up the namespace mapping.

My question is, how do I form a binding like this when the namespace mapping is not present (for example, in an attached behaviour)?

Many thanks in advance!

Upvotes: 7

Views: 2928

Answers (1)

Kent Boogaart
Kent Boogaart

Reputation: 178770

You would specify a full PropertyPath:

var propertyInfo = typeof(IViewModel).GetProperty("Property");
var propertyPath = new PropertyPath("(0)", propertyInfo);
var binding = new Binding
{
    Path = propertyPath
};

For details on the syntax passed to PropertyPath above, see PropertyPath.Path.

Upvotes: 11

Related Questions