Dear colleagues, I try to bind the data to the ListView without behind-code (In the sense of binding itself).
Simple page:

<Page x:Class="App1.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:App1" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid> <ListView x:Name="MyList" ItemsSource="{Binding collection}"> <TextBlock Text="{Binding}"/> </ListView> </Grid> </Page> 

behind-code:

 using System.Collections.ObjectModel; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation; namespace App1 { public sealed partial class MainPage : Page { private ObservableCollection<string> collection; public MainPage() { this.InitializeComponent(); this.DataContext = this; this.NavigationCacheMode = NavigationCacheMode.Required; } protected override void OnNavigatedTo(NavigationEventArgs e) { //ΠŸΡ€ΠΈ Π½Π°Π²ΠΈΠ³Π°Ρ†ΠΈΠΈ Π½Π° страницу ΠΊΠ°ΠΊΠΈΠΌ-Π»ΠΈΠ±ΠΎ ΠΎΠ±Ρ€Π°Π·ΠΎΠΌ ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ ΠΊΠΎΠ»Π»Π΅ΠΊΡ†ΠΈΡŽ. НапримСр Ρ‚Π°ΠΊΠΈΠΌ: collection = new ObservableCollection<string>( new string[] { "One", "Two", "Three"} ); /* Π’ΠΎΡ‚ Ρ‚Π°ΠΊ Ρ€Π°Π±ΠΎΡ‚Π°Π΅Ρ‚: MyList.ItemsSource = collection; */ } } } 

The problem is that the data is not bound. If you explicitly set the ItemsSource in behind-code as in the commented line, then everything works.
In Google, I found that I need to register this.DataContext = this; but apparently this is not enough. What else is missing in my code to work without behind-code?

    2 answers 2

    First, you have a private collection .

    Secondly, you need a property for binding, that is:

     public ObservableCollection<string> collection {get; private set} 
    • one
      Thank you, this helped solve the problem. I will add that this.DataContext = this; should be earlier than initialization, that is, you need to swap these lines. - user200141
    • one
      You are welcome! Glad to help - Gardes

    Binding occurs to the DataContext properties, not to fields.