X:绑定不适用于ListView和ObservableCollection
我想阅读一些来自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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用Onige Binding绑定到章节:
然后稍后替换章节:
X:BIND默认情况下是Ontime。还不清楚是否设置了章节以派遣属性换式通知。如果不是,那么绑定将不会在属性上更改。
You bind to Chapters using a OneTime binding:
then later replace Chapters:
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.
与其在这样的运行时创建一个新集合:
...您应该修改已经存在的集合:
删除设置器以确保您的初始集合永远不会替换:
如果您在每个更新中替换了一个集合,则首先没有理由使用
observableCollection&lt; t&gt;
。Instead of creating a new collection at runtime like this:
...you should modify the already existing collection:
Remove the setter to make sure that your initial collection is never replaced:
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.您应该使用templateBinding而代替
https:// https:// .microsoft.com/en-us/windows/uwp/xaml-platform/templatebinding-markup-extension
You should use TemplateBinding instead
https://learn.microsoft.com/en-us/windows/uwp/xaml-platform/templatebinding-markup-extension