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

I am trying to bind to an explicitly implemented property of an interface from code. The reason for code binding is that the path to the related property can only be determined at runtime.

In XAML, you can link (for example, in MainWindow.xaml):

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

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

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

since WPF can display a namespace mapping.

My question is, how can I create a binding like this when the namespace mapping is missing (e.g. in the attached behavior)?

Thank you very much in advance!

+6
source share
1 answer

You must specify the full PropertyPath :

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

For more information on the syntax passed in PropertyPath above, see PropertyPath.Path .

+9
source

Source: https://habr.com/ru/post/927536/


All Articles