Friday 7 March 2008

WPF Binding Fails With Objects With Internal Properties

I created an executable Windows Presentation Foundation (WPF) assembly with a WPF window and a simple object with a couple of string properties to bind the form to.

In the window, I set the text property of a textbox to "{Binding Path=SomeValue}" where SomeValue was a property in my class called Something.

In the load event handler of the form I created a new instance of Something called something and set "DataContext = something;", which would set the data context of the window to the instance of Something.

This should have been all I needed to do to run the app and the window should open displaying the SomeValue property of something in the text box.

The app ran without any errors, no security or permission warnings or compile time errors or anything, but the text box was empty.

If I put a break on where I was setting the data context, I could see that SomeValue of something did have a value.

I eventually found out that, even though both the class and the WPF window are in the same assembly, the binding failed because the SomeValue property on Something was marked with an "internal" access modifier.

Changing the access modifier to "public" on that property got the application working, and the text box now displays the SomeValue property of something.

It did surprise me that there was no compilation or runtime error for this one, but there wasn't.

No comments: