lav_shaun
lav_shaun

Reputation: 81

WinUI ListView ObservableColellection binding not working

I'm learning the new WinUI platform together with XAML and GUI programming in C# and I'm not sure if I correctly understand how the bindings and updates work, but I can't get the following code (minimal working example) to work: MainPage.xaml

<Page
    x:Class="uitest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
        <ListView ItemsSource="{x:Bind Items, Mode=OneWay}"></ListView>
        <Button Content="Add" Click="Button_OnClick"></Button>
    </StackPanel>
</Page>

MainPage.xaml.cs

using System.Collections.ObjectModel;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;

namespace uitest
{
    public sealed partial class MainPage : Page
    {
        public ObservableCollection<string> Items = new ObservableCollection<string>();
        
        public MainPage()
        {
            this.InitializeComponent();
            Items.Add("A");
            Items.Add("B");
        }

        private void Button_OnClick(object sender, RoutedEventArgs e)
        {
            Items.Add("Next");
        }
    }
}

When I start the application, I see a list with two items but when I click the button nothing happens. I wanted to use the Live Visual Tree for debugging but it's not working for some reason.

Upvotes: 1

Views: 1085

Answers (1)

lav_shaun
lav_shaun

Reputation: 81

I opened an issue on the official WinUI repository for this and apparently it's a compatibility fault in the latest WinUI 3 preview. I should also note this does not work only on the UWP platform but it works on the Desktop (Win32).

Upvotes: 1

Related Questions