[英]How to Bind the the value of a TextBlock which is inside a DataTemplate?
Hi I want to bind the value of a textBlock which is inside a DataTemplate the text property of the TextBlock will change runtime according to the file/folder listing. 嗨,我想绑定在DataTemplate内部的textBlock的值,TextBlock的text属性将根据文件/文件夹列表更改运行时。 I have written below code but the string empty. 我已经在下面的代码中写了,但是字符串为空。 My working env is Windows Phone 8 with Visual Studio 2012. 我的工作环境是带有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>
this textBlock name= tbfooter has to be updated runtime with Footertext value. 此textBlock name = tbfooter必须使用Footertext值更新运行时。
Now in my code behind I have defined this property like 现在在我的代码后面,我已经定义了这个属性,例如
private int _footerText;
public int FooterText
{
get
{
return this._footerText;
}
set
{
this._footerText=value
NotifyPropertyChanged("FooterText");
}
}
However the value of teh textBlock tbfooter is null, it is not showing anything it is just null. 但是,textBlock tbfooter的值是null,它没有显示任何只是null的值。 Can anybody help me out please ? 有人可以帮我吗?
Edit: I have again updated XAML code here,. 编辑:我再次在这里更新了XAML代码。 I don't follow MVVM here, it is simple windows phone apps. 我在这里不遵循MVVM,它是简单的Windows Phone应用程序。 Any help is appreciated. 任何帮助表示赞赏。
private int _footerText;
public int FooterText
{
get
{
return this._footerText;
}
set
{
this._footerText=value; // <<-----------You might miss this!
NotifyPropertyChanged("FooterText");
}
}
It looks like that in your property setter you need to set value before notifying about its change, please try something like this 看起来在属性设置器中,您需要先设置值,然后再通知其更改,请尝试以下操作
private int _footerText;
public int FooterText
{
get
{
return this._footerText;
}
set
{
this._footerText = value;
NotifyPropertyChanged("FooterText");
}
}
When using a DataTemplate
the DataContext
of the DataTemplate
is the current selected Item. 当使用DataTemplate
所述DataContext
所述的DataTemplate
是当前选择的项目。 If you are binding a LongListSelector
to a List of Type T you can access the properies via binding of this type T. 如果将LongListSelector
绑定到类型T的列表,则可以通过绑定此类型T来访问LongListSelector
。
You want to bind to a property of your Viewmodel which is not the current DataContext. 您想绑定到Viewmodel的属性,该属性不是当前的DataContext。 For this reason your result is null. 因此,您的结果为空。
Try this code 试试这个代码
<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>
As mentioned by inxs, your TextBlock
is empty because it's not binding to the right property. 如inxs所述,您的TextBlock
为空,因为它没有绑定到正确的属性。 Take a look at this answer , it illustrates how to bind to both properties on the DataContext
and to a property in the code-behind. 看看这个答案 ,它说明了如何绑定到DataContext
上的两个属性以及背后代码中的一个属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.