【发布时间】:2014-01-30 15:48:17
【问题描述】:
我正在开发 windows phone 8 应用程序。我有一个客户UserControl 叫SelectableButton。它的构造函数如下:
public SelectableButton()
{
InitializeComponent();
DataContext = this;
}
它的xaml是这样的:
<Grid>
<TextBlock x:Name="ButtonTextBlock"
Text="{Binding SelectableButtonText, Mode=TwoWay}"
SomeOtherCode
/>
...
</Grid>
SelectableButtonText 是这个UserControl 的属性:
public static readonly DependencyProperty SelectableButtonTextProperty =
DependencyProperty.Register(
"SelectableButtonText", typeof(string),
typeof(SelectableButton),
null
);
现在我在Pivot 中使用这个SelectableButton。我想将SelectableButtonText 属性绑定到一些数据。这是Pivot 中使用的DataTemplate,称为PivotTestContent:
<ShareControl:SelectableButton
SelectableButtonText="{Binding question}"
...
>
</ShareControl:SelectableButton>
question 来自这个ItemsSource 的Pivot:
PivotTestContent.ItemsSource = quizs;
quizs 是 List<> 的 WCCQuizText
quizs = new List<WCCQuizText>();
而question 是WCCQuizText 的属性成员:
public String question
{
get;
set;
}
完成所有这些工作后,我发现Binding 找不到属性question。似乎是因为SelectableButton的构造函数中的这一行:
DataContext = this;
绑定将在SelectableButton 类中查找属性question,而不是从ItemsSouce。因为如果我将question 直接绑定到某个TextBlock.Text,它将起作用。但是当我将它绑定到我的 UserControl 时,它就找不到了。
那么有人知道如何处理吗?
如果我这样做,我可以正确显示绑定文本,TextBlock 也在 Pivot 中。
<TextBlock
Name="TextBlockQuestion"
Text="{Binding question}"
....
>
</TextBlock>
还有我的绑定:
<ShareControl:SelectableButton
SelectableButtonText="{Binding Text, ElementName=TextBlockQuestion}"
....
>
</ShareControl:SelectableButton>
【问题讨论】:
标签: c# windows-phone-7 data-binding windows-phone-8