简体   繁体   English

WP7 / XAML:数据绑定到代码隐藏文件中的属性

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

I'm rather new to XAML and Silverlight. 我是XAML和Silverlight的新手。 I have a XAML page and a code behind class for it. 我有一个XAML页面和一个类后面的代码。 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." 尝试将XAML的根元素指定为DataContext(按名称,作为ElementName)会导致设计器错误“值不在预期范围内”。

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? 基地( PhoneApplicationPage )中没有一个,但我定义的那个?

Your code behind should be the datacontext. 您的代码应该是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. 您应该能够绑定到受保护的属性,但只能绑定一种方式,即从属性到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. 因为它是只读的,所以如果用户在页面上更改了值,您将无法获得该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM