ItemsControl List <string> -Binding不起作用

时间:2015-12-31 03:16:39

标签: c# xaml binding windows-phone-8.1 itemscontrol

我使用this作为我的模板,但Windows Phone模拟器中没有显示任何内容。我正在尝试将字符串列表绑定到ItemsControl。我做的时候不会工作。我也试图使用StackPanel,但我删除了试图让它工作。

PivotPage.xaml

<Page
    x:Class="WinPhone8__Practice.PivotPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:WinPhone8__Practice"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:data="using:WinPhone8__Practice.Data"
    mc:Ignorable="d"
    DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Page.Transitions>
        <TransitionCollection>
            <NavigationThemeTransition>
                <NavigationThemeTransition.DefaultNavigationTransitionInfo>
                    <CommonNavigationTransitionInfo IsStaggeringEnabled="True"/>
                </NavigationThemeTransition.DefaultNavigationTransitionInfo>
            </NavigationThemeTransition>
        </TransitionCollection>
    </Page.Transitions>

    <Grid>
        <Pivot x:Uid="Pivot" Title="MY APPLICATION" x:Name="pivot" CommonNavigationTransitionInfo.IsStaggerElement="True">
            <!--Pivot item one-->
            <PivotItem
                x:Uid="PivotItem1"
                Margin="19,14.5,0,0"
                CommonNavigationTransitionInfo.IsStaggerElement="True">
                <!--Double line list with text wrapping-->
                <ItemsControl ItemsSource="{Binding strings}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding}"/>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </PivotItem>
        </Pivot>
    </Grid>
</Page>

PivotPage.xaml.cs

 public sealed partial class PivotPage : Page
    {
        private readonly NavigationHelper navigationHelper;
        public List<string> strings { get; private set; }
        public PivotPage()
        {
            this.InitializeComponent();
            strings = new List<string>() { "Yes", "No", "Maybe", "I don't know", "Can you repeat the question?" };

            this.NavigationCacheMode = NavigationCacheMode.Required;

            this.navigationHelper = new NavigationHelper(this);
            this.navigationHelper.LoadState += this.NavigationHelper_LoadState;
            this.navigationHelper.SaveState += this.NavigationHelper_SaveState;
        }

        /// <summary>
        /// Gets the <see cref="NavigationHelper"/> associated with this <see cref="Page"/>.
        /// </summary>
        public NavigationHelper NavigationHelper
        {
            get { return this.navigationHelper; }
        }

        private async void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
        {
            await DoNothing();
        }

        private void NavigationHelper_SaveState(object sender, SaveStateEventArgs e)
        {
            // TODO: Save the unique state of the page here.
        }

        private Task DoNothing() { return new Task(new Action(() => { })); }
    }

2 个答案:

答案 0 :(得分:1)

使其有效在构造函数中this.DataContext=this;之后添加this.InitializeComponent();

为何需要

  1. 仅当您设置DataContext时,Bindings才会起作用;
  2. DataContext的默认值为null;
  3. 使用此this.DataContext=this;,您将DataContext初始化为同一个类。

答案 1 :(得分:0)

  1. 首先摆脱XAML中的ItemsSource="{Binding strings}"
  2. 当您使用{Binding something}必须与您的财产名称相同时
  3. 执行此操作的最佳方法是使用道具

    创建班级模型

    public class Model { public string Names{ get; set; } }

  4. 在您的代码中,您创建了ListModel

    public List<Model> model = new List<Model>();

  5. 创建一个方法来填充字段。

    public void ListModel() { model.Add(new Model { Names = "Hello" }); }

  6. 在主空白处,您拨打ListModel();PivotItem1.ItemsSource = model

  7. 您的XAML将是:

    <Pivot x:Uid="Pivot" Title="MY APPLICATION" x:Name="pivot" CommonNavigationTransitionInfo.IsStaggerElement="True"> <!--Pivot item one--> <PivotItem x:Uid="PivotItem1" Margin="19,14.5,0,0" ItemsSource={Biding} CommonNavigationTransitionInfo.IsStaggerElement="True"> <!--Double line list with text wrapping--> <ItemsControl> <ItemsControl.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Names}" Foreground = "Black"/> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </PivotItem> </Pivot>