2013-04-26 67 views
3

我在LongListSelector的ListFooterTemplate的數據模板中有一個TextBlock,我將Collection作爲Itemssource發送給我,我想將TextBlock的Text屬性綁定到Codebehind中的一個字符串。請告訴我該怎麼做。這是xaml。 我正在使用VS2012和WP8 SDK。如何在LongListSelector的Listfootertemplate的dataTempalte中綁定文本塊的文本值

<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屬性,將導致此問題上。

下面是一些示例代碼,爲我的作品:

代碼隱藏

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

謝謝你這麼有魅力much.Works。 – Kumar 2013-05-02 08:58:43