开发者

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

开发者 https://www.devze.com 2023-02-23 08:17 出处:网络
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? T

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消