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?