简体   繁体   English

ItemsControl清单 <string> -绑定将不起作用

[英]ItemsControl List<string>-Binding Won't Work

I used this as my template, but nothing shows in the Windows Phone Emulator. 我将用作模板,但Windows Phone Emulator中没有任何显示。 I'm trying to bind a list of strings to an ItemsControl . 我正在尝试将字符串列表绑定到ItemsControl Won't work when I do it. 当我这样做时将无法正常工作。 I'm also trying to use a StackPanel , but I removed to try to get this to work. 我也在尝试使用StackPanel ,但是为了StackPanel ,我删除了它。

PivotPage.xaml : 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 : 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(() => { })); }
    }

To make it work Add this.DataContext=this; 要使其工作,请添加this.DataContext=this; after this.InitializeComponent(); 之后this.InitializeComponent(); in the constructor. 在构造函数中。

Why it is needed 为什么需要它

  1. The Bindings will work only if you sets the DataContext; 只有设置了DataContext,绑定才起作​​用。
  2. The default value of DataContext is null; DataContext的默认值为null。
  3. By using this this.DataContext=this; 通过使用this.DataContext=this; you are initializing the DataContext as the same class. 您正在将DataContext初始化为同一类。
  1. First get rid of the ItemsSource="{Binding strings}" inside your XAML. 首先摆脱XAML内部的ItemsSource="{Binding strings}"
  2. When you use {Binding something} has to be the same name of your property 当您使用{Binding something} ,必须与您的媒体资源名称相同
  3. The best way to do this is to Create a Class Model with your props 最好的方法是使用道具创建类模型

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

  4. In your code behind you create a List of your Class Model 在后面的代码中,创建类ModelList

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

  5. Create a method to pupulate the fields. 创建一种方法来化字段。

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

  6. In your Main void you call ListModel(); 在您的Main void中,调用ListModel(); and PivotItem1.ItemsSource = model . PivotItem1.ItemsSource = model

  7. Your XAML will be : 您的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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM