3

I have a TextBlock in the datatemplate of ListFooterTemplate of LongListSelector,to which I give a Collection as Itemssource,I want to bind the Text Property of TextBlock to a string in the Codebehind. Please tell me how to do it. Here is the xaml. I am using VS2012 and WP8 SDK.

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

footertext is the string I have defined in the codebehind. I have implemented INotifyPropertyChanged also but footer doesnt show the text.

4

1 回答 1

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属性,而页脚绑定到代码隐藏类中的属性。LongListSelectorDataContext

希望这可以帮助。

于 2013-04-29T19:48:21.533 回答