X:绑定不适用于ListView和ObservableCollection

发布于 2025-01-28 22:51:11 字数 773 浏览 2 评论 0原文

我想阅读一些来自SQLite的数据并将其绑定到ListView 这是我的代码:

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

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

我的XAML

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

,但我的视图没有更新,我看不到项目。 哪里错了?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情深已缘浅 2025-02-04 22:51:11

您可以使用Onige Binding绑定到章节:

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

然后稍后替换章节:

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

X:BIND默认情况下是Ontime。还不清楚是否设置了章节以派遣属性换式通知。如果不是,那么绑定将不会在属性上更改。

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.

放手` 2025-02-04 22:51:11

与其在这样的运行时创建一个新集合:

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

...您应该修改已经存在的集合:

var chapters = await db.Chapters.ToListAsync();
Chapters.Clear();
if (chapters != null)
foreach (var chapter in chapters)
     Chapers.Add(chapter);

删除设置器以确保您的初始集合永远不会替换:

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

如果您在每个更新中替换了一个集合,则首先没有理由使用observableCollection&lt; t&gt;

Instead of creating a new collection at runtime like this:

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

...you should modify the already existing collection:

var chapters = await db.Chapters.ToListAsync();
Chapters.Clear();
if (chapters != null)
foreach (var chapter in chapters)
     Chapers.Add(chapter);

Remove the setter to make sure that your initial collection is never replaced:

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

If you replace the collection with another one each on each update, there is no reason to use an ObservableCollection<T> in the first place.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文