MVVM 中列表框刷新出现问题
好吧,学习 MVVM 让我抓狂。我知道这是一个很好的模式,但有时......
我有简单的图书编目应用程序。首先使用 EF 代码完成模型。它包含两个表authors
和books
。现在,我有 MainWindow
,后面有 MainWindowViewModel
。在 MainWindow 中,我有一个绑定到 ViewModel 的列表框,如下所示:
<ListBox ItemContainerStyle="{DynamicResource ListBoxItemStyle1}"
ItemsSource="{Binding Authors, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="Fullname"Name="AuthorsListBox" sSynchronizedWithCurrentItem="True"/>
其中“Authors”是我的 DataContext (ViewModel) 中的 ObservableCollection
。目前看来一切都很好。错误是:
我正在打开新窗口,非常简单,只有两个文本框和按钮来创建新作者。
验证后,我单击按钮,新作者实体将保存到数据库中。然后,我关闭“CreateAuthorWindow”并返回到“MainWindow”。
列表框中没有显示新的实体。而且我无法让它显示!我的模型中的所有内容都实现了 INotifyPropertyChanged。有什么方法可以做到这一点而不需要手动刷新吗? (实际上这也不起作用......)
Ok, learning MVVM drives me mad. I know it's good pattern, I know, but sometimes...
I have simple book cataloging app. Model done with EF code first. It contains two tables authors
and books
. Now, I have MainWindow
with MainWindowViewModel
behind it. In MainWindow I have a Listbox bound to ViewModel like this:
<ListBox ItemContainerStyle="{DynamicResource ListBoxItemStyle1}"
ItemsSource="{Binding Authors, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="Fullname"Name="AuthorsListBox" sSynchronizedWithCurrentItem="True"/>
Where "Authors" is ObservableCollection<Author>
in my DataContext (ViewModel). Everything seems fine for now. What is wrong is:
I'm opening new window, simple as hell, only two textboxes and button to create new author
After validation I click the button and new Author entity is saved to database. Then, I close "CreateAuthorWindow"
and back in MainWindow
.
There is new no entity showing in listbox. And I can't get it show! Everything in my model implements INotifyPropertyChanged
. Is there any way to do this without refreshing by hand? (which actually doesn't work either...)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将新作者保存到数据库是不够的,您需要 A) 从数据库重新加载作者或 B) 将新创建的作者对象添加到现有列表中。
Saving the new Author to the database will not be enough, you will need to either A) reload Authors from the database or B) add the newly created Author object to the existing list.
要使新作者显示在列表框中,您需要确保:
ListBox
的 DataContext。如果您已将 ViewModel 设置为 View 的 DataContext 那么就可以了。Authors
属性必须是公共的,并且在集合更改(添加或删除项目)时必须引发 NotifyCollectionChanged 事件。ObservableCollection
将为您完成此操作。NotifyCollectionChanged
事件,并且视图应该刷新。如果您执行了所有这些操作,但仍然没有看到新项目出现,请检查调试窗口中的绑定错误。
如果仍然无法正常工作,您可能需要发布一些代码...
To get the new Author to show up in the list box, you need to make sure:
ListBox
is set. If you have set your ViewModel as the DataContext of the View then that will be fine.Authors
property must be public, and must raise a NotifyCollectionChanged event when the collection is changed (items are added or removed). AnObservableCollection<T>
will do this for you.NotifyCollectionChanged
event, and the View should refresh.If you are doing all these things and still not seeing the new item turn up, check for binding errors in your debug window.
If it's still not working, you may need to post some code...