Search code examples
c#xamlbindingwindows-phone-8.1itemscontrol

ItemsControl List<string>-Binding Won't Work


I used this as my template, but nothing shows in the Windows Phone Emulator. I'm trying to bind a list of strings to an 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.

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

Solution

  • To make it work Add this.DataContext=this; after this.InitializeComponent(); in the constructor.

    Why it is needed

    1. The Bindings will work only if you sets the DataContext;
    2. The default value of DataContext is null;
    3. By using this this.DataContext=this; you are initializing the DataContext as the same class.