[英]Getting value of TextBlock inside AutoCompleteBox DataTemplate
[英]How to Bind the the value of a TextBlock which is inside a DataTemplate?
嗨,我想绑定在DataTemplate内部的textBlock的值,TextBlock的text属性将根据文件/文件夹列表更改运行时。 我已经在下面的代码中写了,但是字符串为空。 我的工作环境是带有Visual Studio 2012的Windows Phone 8。
<Grid x:Name="ContentPanel">
<phone:LongListSelector>
<phone:LongListSelector.ListFooterTemplate >
<DataTemplate >
<TextBlock Name="tbfooter" Text="{Binding FooterText, Mode=OneWay}" />
</DataTemplate>
</phone:LongListSelector.ListFooterTemplate>
</phone:LongListSelector>
此textBlock name = tbfooter必须使用Footertext值更新运行时。
现在在我的代码后面,我已经定义了这个属性,例如
private int _footerText;
public int FooterText
{
get
{
return this._footerText;
}
set
{
this._footerText=value
NotifyPropertyChanged("FooterText");
}
}
但是,textBlock tbfooter的值是null,它没有显示任何只是null的值。 有人可以帮我吗?
编辑:我再次在这里更新了XAML代码。 我在这里不遵循MVVM,它是简单的Windows Phone应用程序。 任何帮助表示赞赏。
private int _footerText;
public int FooterText
{
get
{
return this._footerText;
}
set
{
this._footerText=value; // <<-----------You might miss this!
NotifyPropertyChanged("FooterText");
}
}
看起来在属性设置器中,您需要先设置值,然后再通知其更改,请尝试以下操作
private int _footerText;
public int FooterText
{
get
{
return this._footerText;
}
set
{
this._footerText = value;
NotifyPropertyChanged("FooterText");
}
}
当使用DataTemplate
所述DataContext
所述的DataTemplate
是当前选择的项目。 如果将LongListSelector
绑定到类型T的列表,则可以通过绑定此类型T来访问LongListSelector
。
您想绑定到Viewmodel的属性,该属性不是当前的DataContext。 因此,您的结果为空。
试试这个代码
<Grid x:Name="ContentPanel">
<phone:LongListSelector>
<phone:LongListSelector.ListFooterTemplate >
<DataTemplate >
<TextBlock Name="tbfooter"
DataContext="{Binding Path=DataContext,RelativeSource={RelativeSource AncestorType=UserControl}}"
Text="{Binding FooterText, Mode=OneWay}" />
</DataTemplate>
</phone:LongListSelector.ListFooterTemplate>
</phone:LongListSelector>
如inxs所述,您的TextBlock
为空,因为它没有绑定到正确的属性。 看看这个答案 ,它说明了如何绑定到DataContext
上的两个属性以及背后代码中的一个属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.