2013-04-25 187 views
0

您好我想绑定一个DataTemplate内的textBlock的值,TextBlock的text属性将根据文件/文件夹列表更改运行时。我写了下面的代码,但字符串为空。 我的工作ENV是的Windows Phone 8与Visual Studio 2012如何绑定DataTemplate中的TextBlock的值?

<Grid x:Name="ContentPanel"> 
<phone:LongListSelector>  
    <phone:LongListSelector.ListFooterTemplate > 
     <DataTemplate > 
      <TextBlock Name="tbfooter" Text="{Binding FooterText, Mode=OneWay}" /> 
     </DataTemplate> 
    </phone:LongListSelector.ListFooterTemplate> 
</phone:LongListSelector> 

这个文本块名= tbfooter必须与Footertext值更新运行。

现在在我的代码隐藏我已经定义了这个属性好像

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

但是德文本块tbfooter的值为null,它没有显示任何东西它只是空。有人可以帮我吗?

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

+2

也许你错过了this._footerText =属性设置器内的值。 – jure 2013-04-25 09:03:04

+0

感谢您的纠正,但我仍然无法获得价值。在我看来,TextBlock名称= tbfooter,不能从CodeBehind访问,原因何在? – Debhere 2013-04-25 09:13:57

回答

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

它看起来像在你的属性设置你需要通知有关其改变之前达到设定值时,请尝试这样的事情

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

感谢您的纠正,但我仍然无法获得价值。在我看来,TextBlock的tbfooter名称不能从CodeBehind访问,原因何在? – Debhere 2013-04-25 09:13:18

0

当使用DataTemplateDataTemplateDataContext在当前选定的项目。如果您将LongListSelector绑定到T类型的列表,则可以通过此类型T的绑定来访问适当的属性。

您想绑定到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> 
0

正如INXS提到的,你TextBlock是空的,因为它不绑定到合适的物业。看看this answer,它说明了如何绑定到DataContext上的两个属性以及代码隐藏中的属性。

相关问题