0
votes

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. I have written below code but the string empty. My working env is Windows Phone 8 with Visual Studio 2012.

<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.

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. Can anybody help me out please ?

Edit: I have again updated XAML code here,. I don't follow MVVM here, it is simple windows phone apps. Any help is appreciated.

4
Maybe your are missing this._footerText=value inside property setter.Jurica Smircic
Thanks for the correction but still I can't get the value. It seems to me that the TextBlock Name =tbfooter, is not accesible from CodeBehind any reason why?Debhere

4 Answers

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

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");
   }
}
0
votes

When using a DataTemplate the DataContext of the DataTemplate is the current selected Item. If you are binding a LongListSelector to a List of Type T you can access the properies via binding of this type T.

You want to bind to a property of your Viewmodel which is not the current 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> 
0
votes

As mentioned by inxs, your TextBlock is empty because it's not binding to the right property. 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.