我有一個簡單的自定義用戶控件:結合依賴屬性
<Grid x:Name="LayoutRoot">
<StackPanel>
<TextBlock Text="{Binding DisplayText}"/>
<TextBlock Text="{Binding PersonName}"/>
</StackPanel>
</Grid>
我用它在MainPage.xaml中,如下所示:
<local:WindowsPhoneControl1 x:Name="customControl" DisplayText="test">
</local:WindowsPhoneControl1>
如果MainPage.xaml.cs中我做的:
PersonName = "George";
customControl.DataContext = this;
然後喬治顯示,但測試不是。這是有道理的,但我不知道如何綁定到DisplayText屬性。
當然,以下是行不通的,因爲喬治不會被顯示:
customControl.DataContext = customControl;
請注意,這應該WP/Silverlight開發工作,所以像AncestorType的東西可能不可用(不一定有用)
您應該閱讀這篇關於依賴項屬性值優先級的文章:http://msdn.microsoft.com/en-us/library/ms743230.aspx – Guillaume