2013-04-26 3 views
3

У меня есть TextBlock в datatemplate ListFooterTemplate из LongListSelector, которому я даю Collection как Itemssource, я хочу привязать текстовое свойство TextBlock к строка в Codebehind. Скажите, пожалуйста, как это сделать. Вот xaml. Я использую VS2012 и WP8 SDK.Как связать текстовое значение текстового блока внутри dataTempalte Listfootertemplate Longlistselector

<toolkit:LongListSelector ItemsSource="{Binding Collection}"> 
    <toolkit:LongListSelector.ListFooterTemplate> 
     <DataTemplate> 
     <TextBlock Text= "{Binding footertext}" /> 
     </DataTemplate> 
    </toolkit:LongListSelector.ListFooterTemplate> 
    </toolkit:LongListSelector> 

footertext - это строка, которую я определил в коде. Я также внедрил INotifyPropertyChanged, но нижний колонтитул не отображает текст.

ответ

1

Просто угадывая здесь, но наиболее вероятная причина, по которой вы не видите нижний колонтитул, заключается в том, что вы не привязываетесь к правильному объекту. LongListSelector связывается с свойствами на своем DataContext. Если свойство Collection живет на другом объекте, кроме свойства footertext, которое может вызвать эту проблему.

Вот пример кода, который работает для меня:

Code-за

namespace LongListSelector 
{ 
    public partial class MainPage : PhoneApplicationPage 
    { 
     // Constructor 
     public MainPage() 
     { 
      InitializeComponent(); 

      SomeText = "This is my footer text from the code-behind"; 
     } 

     public string SomeText { get; private set; } 
    } 
} 

XAML

<phone:PhoneApplicationPage 
    x:Class="LongListSelector.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:LongListSelector" 
    mc:Ignorable="d" 
    x:Name="page" 
    FontFamily="{StaticResource PhoneFontFamilyNormal}" 
    FontSize="{StaticResource PhoneFontSizeNormal}" 
    Foreground="{StaticResource PhoneForegroundBrush}" 
    SupportedOrientations="Portrait" Orientation="Portrait" 
    shell:SystemTray.IsVisible="True"> 

    <phone:PhoneApplicationPage.DataContext> 
     <local:SampleData/> 
    </phone:PhoneApplicationPage.DataContext> 

    <!--LayoutRoot is the root grid where all page content is placed--> 
    <Grid x:Name="LayoutRoot" Background="Transparent"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 

     <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> 
      <TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/> 
      <TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 
     </StackPanel> 

     <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
      <phone:LongListSelector ItemsSource="{Binding Collection}"> 
       <phone:LongListSelector.ItemTemplate> 
        <DataTemplate> 
         <TextBlock Text="{Binding}"/> 
        </DataTemplate> 
       </phone:LongListSelector.ItemTemplate> 
       <phone:LongListSelector.ListFooterTemplate> 
        <DataTemplate> 
         <TextBlock Text="{Binding SomeText,ElementName=page}"/> 
        </DataTemplate> 
       </phone:LongListSelector.ListFooterTemplate> 
       <phone:LongListSelector.ListHeaderTemplate> 
        <DataTemplate> 
         <TextBlock Text="{Binding DataContext.HeaderText, ElementName=page, Mode=OneWay}"/> 
        </DataTemplate> 
       </phone:LongListSelector.ListHeaderTemplate> 
      </phone:LongListSelector> 
     </Grid> 
    </Grid> 

</phone:PhoneApplicationPage> 

Выборочные данные объекта

using System.Collections.ObjectModel; 

namespace LongListSelector 
{ 
    public class SampleData 
    { 
     public SampleData() 
     { 
      Collection = new ObservableCollection<string>(new string[] { "Item 1", "Item 2", "Item 3" }); 
      HeaderText = "This is my header text"; 
     } 

     public ObservableCollection<string> Collection { get; private set; } 

     public string HeaderText { get; private set; } 
    } 
} 

Обратите внимание, что ItemsSource свойства на LongListSelector связывается с DataContext (как заголовок), а сноска связывается со свойством в коде-за классом.

Надеюсь, это поможет.

+0

Большое вам спасибо. Работает как очарование. – Kumar