如何绑定DataTemplate中的TextBlock的值?

时间:2013-04-25 08:57:56

标签: c# xaml data-binding windows-phone-8

您好我想绑定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");
   }
}

然而,teh textBlock tbfooter的值为null,它没有显示任何它只是null。有人可以帮帮我吗?

编辑:我在这里再次更新了XAML代码。我这里不关注MVVM,它是简单的Windows手机应用程序。任何帮助表示赞赏。

4 个答案:

答案 0 :(得分:0)

private int _footerText;
public int FooterText
{
   get
   {
      return this._footerText;
   }
   set
   {
      this._footerText=value;  //  <<-----------You might miss this!
      NotifyPropertyChanged("FooterText");
   }
}

答案 1 :(得分:0)

在您的属性设置器中看起来您需要在通知其更改之前设置值,请尝试这样的事情

private int _footerText;
public int FooterText
{
   get
   {
      return this._footerText;
   }
   set
   {  
      this._footerText = value;
      NotifyPropertyChanged("FooterText");
   }
}

答案 2 :(得分:0)

使用DataTemplateDataContext的{​​{1}}是当前所选项目。如果要将DataTemplate绑定到类型T的列表,则可以通过绑定此类型T来访问属性。

您希望绑定到Viewmodel的属性,该属性不是当前的DataContext。因此,您的结果为空。

试试此代码

LongListSelector

答案 3 :(得分:0)

正如inxs所提到的,你的TextBlock是空的,因为它没有绑定到正确的属性。看看this answer,它说明了如何绑定DataContext上的两个属性以及代码隐藏中的属性。

相关问题