Home > Software design >  x:Bind is not working with Listview and ObservableCollection
x:Bind is not working with Listview and ObservableCollection

Time:05-17

i want to read some data from sqlite and bind them to listview this is my codes:

public ObservableCollection<ChapterProperty> Chapters { get; set; } = new();

using var db = new AlAnvarDBContext();
Chapters = new ObservableCollection<ChapterProperty>(await db.Chapters.ToListAsync());

and my xaml

<ListView ItemsSource="{x:Bind Chapters}">
            <ListView.ItemTemplate>
                <DataTemplate x:DataType="tables:ChapterProperty">
                    <StackPanel>
                        <TextBlock Text="{x:Bind Name}"/>
                    </StackPanel>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

but my view is not updated and i cant see items. where is wrong?

CodePudding user response:

You bind to Chapters using a OneTime binding:

<ListView ItemsSource="{x:Bind Chapters}">

then later replace Chapters:

Chapters = new ObservableCollection<ChapterProperty>(await db.Chapters.ToListAsync());

x:Bind is OneTime by default. Also unclear if Chapters is setup to dispatch PropertyChanged notifications. If it isn't, then the binding wouldn't update on a property change anyways.

CodePudding user response:

You should use TemplateBinding instead

https://docs.microsoft.com/en-us/windows/uwp/xaml-platform/templatebinding-markup-extension

  • Related