ItemsControl List<string>-绑定无效

ItemsControl List<string>-Binding Won't Work

我使用 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(() => { })); }
    }

要使其工作,请在构造函数中的 this.InitializeComponent(); 之后添加 this.DataContext=this;

为什么需要

  1. 仅当您设置 DataContext 时,绑定才会起作用;
  2. DataContext的默认值为null;
  3. 通过使用此 this.DataContext=this;,您正在将 DataContext 初始化为相同的 class。
  1. 首先去掉 XAML 中的 ItemsSource="{Binding strings}"
  2. 当您使用 {Binding something} 时,必须与您的 属性
  3. 同名
  4. 最好的方法是用道具Class创建一个模型

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

  5. 在您后面的代码中创建一个 Class Model

    List

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

  6. 创建一个填充字段的方法。

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

  7. 在你的 Main void 中你调用 ListModel();PivotItem1.ItemsSource = model.

  8. 您的 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>