windows-phone-8 - 如何在 Longlistselector 的 Listfootertemplate 的 dataTempalte 中绑定(bind)文本 block 的文本值

标签 windows-phone-8 longlistselector

我在 LongListSelector 的 ListFooterTemplate 的数据模板中有一个 TextBlock,我给它一个 Collection 作为 Itemssource,我想将 TextBlock 的 Text 属性绑定(bind)到代码隐藏中的一个字符串。请告诉我该怎么做。这是 xaml。 我正在使用 VS2012 和 WP8 SDK。

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

footertext 是我在代码隐藏中定义的字符串。我也实现了 INotifyPropertyChanged,但页脚不显示文本。

最佳答案

这里只是猜测,但您看不到任何页脚的最可能原因是您没有绑定(bind)到正确的对象。 LongListSelector 绑定(bind)到其 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; }
    }
}

请注意,LongListSelector 上的 ItemsSource 属性绑定(bind)到 DataContext(页眉也是如此),而页脚绑定(bind)到代码隐藏类。

希望这对您有所帮助。

关于windows-phone-8 - 如何在 Longlistselector 的 Listfootertemplate 的 dataTempalte 中绑定(bind)文本 block 的文本值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16232418/

相关文章:

android - 智能手机之间的蓝牙局域网?一对多电话连接?

c# - 将用户在文本框中输入的 URL 转换为 URI 格式

c# - Windows Phone 8 应用程序不包含 InitializeComponent 的定义

c# - 如何在 Windows Phone 8 中访问 UserControl 的组件/元素

memory-leaks - WP8 LongListSelector内存泄漏

silverlight - 为什么 WP8 LongListSelector 会错误地重用 CheckBox 的 Checked 状态?

c# - Windows Phone 应用内购买 - 无法将 TValue 转换为 ProductListing

java - Android 的监听器构造可以在 Windows Phone 中使用吗?

c# - 在 WP8 中打开弹出窗口时,LongListSelector 交互中断

windows-phone-7 - WP7 工具包更新从 LongListSelector 中删除了 GetItemsInView()