I have to create a Binding to read the value of a property of an object that is the value of an attached property.
In XAML I can it this way:
<TextBlock Text="{Binding Path=(myns:MyDependencyObject.MyAttachedProperty).NestedProperty, Mode=OneWay}"/>
How can I do that in C# code?
You can do that via this approach:
var path = new PropertyPath("(0).NestedProperty", MyDependencyObject.MyAttachedProperty);
var binding = new Binding() { Path = path, Mode = BindingMode.OneWay };
myTextBlock.SetBinding(TextBlock.TextProperty, binding);
The constructor used for PropertyPath is the following:
public PropertyPath(string path, params object[] pathParameters)
Please note that "(0)"
is similar to the string.Format
syntax, where (0)
corresponds to the first parameter in the pathParameters
params list.
The Microsoft's docs are not so clear about that.