WPF Binding to a Custom Attached Property in Code

If you have a custom WPF attached property declared that you want to bind to in code, it’s very important to know that the paranthesis syntax won’t work.

For example, if you have a custom attached property AP defined by a class C, this doesn’t work:

Binding b = new Binding("(C.AP)");

Instead you need to do it this way:

Binding b = new Binding();
b.Path = new PropertyPath("(0)", C.APProperty); //using place holder and parameter

I found the solution above in an article on MSDN Forums, after a search of several hours. Microsoft didn’t say it in the MSDN documentation, at least in the data binding overview and related articles I’ve read.

About Sorin Dolha

My passion is software development, but I also like physics.
This entry was posted in Computers and Internet. Bookmark the permalink.

2 Responses to WPF Binding to a Custom Attached Property in Code

  1. bidy says:

    what is “(0)” in your propertypath?

Add a reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s