Seva Alekseyev
Seva Alekseyev

Reputation: 61341

WP7/XAML: data binding to a property in the code-behind file

I'm rather new to XAML and Silverlight. I have a XAML page and a code behind class for it. In the class, I have a protected read-only property. Can I bind a control to that property? Trying to specify the root element of the XAML as the DataContext (by name, as an ElementName) causes a designer error "Value does not fall within the expected range."

EDIT: I'd like to do in the designer-fiendly way. I understand I can do everything (including control population) from code; that's not the point. Can I have the designer recognize and display the properties of my code-behind class? Not one ones from the base (PhoneApplicationPage) but the ones that I define?

Upvotes: 0

Views: 1249

Answers (1)

Steve Chadbourne
Steve Chadbourne

Reputation: 6953

Your code behind should be the datacontext.

For example on a main page code behind:

public MainPage()
{
    InitializeComponent();

    DataContext = this;
}

You should be able to bind to the protected property but only one way ie from the property to the xaml. As it is read-only you will not be able to get the value if it is changed on the page by the user.

Upvotes: 2

Related Questions