将更改的集合绑定到组合框 [英] Binding a changing collection to a combobox

查看:60
本文介绍了将更改的集合绑定到组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,这就是事情。在WPF应用程序中,我填充 BindableCollection (请参阅: Caliburn.Micro [<来自ViewModel构造函数的a href =http://caliburnmicro.com/target =_ blanktitle =New Window> ^ ]。



这是构造函数:



So, here's the thing. In a WPF application, I am populating a BindableCollection (see: Caliburn.Micro[^] ) from the ViewModel constructor.

Here's the constructor:

public UsersViewModel(IUsersUoW uow, IEventAggregator eventAggregator) : base(uow, eventAggregator)
{
    DisplayName = "Users";
    Users = new BindableCollection<User>(_uow.userrepo.FetchAll());
}





简而言之,_uow是我的工作单位,userrepo是我的存储库。因此,这实际上填充了绑定到视图中的Combobox的属性Users。有一个SelectedUser属性,指示ComboBox的所选项。现在,视图中有一个Textbox,它显示了SelectedUser的各种属性。可以通过编辑TextBox中的文本来编辑属性。



现在,为了保存更改,我有一个方法,如下:





In short, _uow is my Unit of Work and userrepo is my repository. So, this actually populates the property Users which is bound to a Combobox in my View. There's a SelectedUser property which indicates the chosen item of the ComboBox. Now, there's a load of Textbox in view which display various properties of SelectedUser. One can edit the properties by editing the text in TextBox.

Now, in order to save the changes, I have a method, like so:

public void Modify()
{
    var currentuser = _uow.userrepo.Find(SelectedUser.ID);
    currentuser.addr = Address;
    //save other properties
    _uow.userrepo.Update(currentuser);
    _uow.Complete();
    Status = "Modified!";
}





问题是,此更改已正确保存在数据库中,但是当我更改所选项目时将ComboBox更改为其他项目,然后将其更改回我编辑的项目,显示过时数据。



存储库的Update方法是:





The problem is, this change is being properly saved in the DB, but when I change the selected item in ComboBox to some other item, and then change it back to the item which I edited, the stale data is displayed.

The Update method of the repository is:

virtual public void Update(T updatedentity)
{
    _ctx.Set<T>().Attach(updatedentity);
    _ctx.Entry(updatedentity).State = EntityState.Modified;
}





我的尝试:



缓解此问题的一种方法是在进行修改后重新填充ComboBox。但这似乎对我来说很糟糕。



避免像这样的陈旧数据的标准协议是什么?我应该像这样使用属性用户吗?





What I have tried:

One way to alleviate this problem is to repopulate the ComboBox once the modification is made. But that seems to kludgy to me.

What is the standard protocol to avoid stale datum like this? Should I make the property Users like this?

public BindableCollection<User> Users => new BindableCollection<User>(_uow.userrepo.FetchAll());





在这种情况下,我认为会有性能损失,因为每次访问ComboBox时,都会访问存储库。



In this case, I think there would be a performance hit, since every time I access the ComboBox, the repository would be accessed.

推荐答案

所以我认为你遇到的问题是你正在修改数据库中的条目,但你没有更新集合本身的条目。 />


在Modify()函数中,您将从数据库中获取当前用户,而不是集合本身。我会尝试从集合中获取当前用户,修改User,然后使用它来修改存储库中的条目。您还必须从集合中删除该项目,然后将修改后的条目添加回集合中。



另外,请小心使用
So the problem that I think you are having is the fact that you are modifying the entry in the database, but you are not updating the entry in the collection itself.

In your Modify() function you are are fetching the current user from the database, not the collection itself. I would try to fetch the current user from the collection, modify that "User" and then use that to modify the entry in the repository. You would also have to remove the item from the collection and then add the modified entry back into the collection.

Also, be careful using
= new BindableCollection<User>(_uow.userrepo.FetchAll());

每次去取,因为

new

可能会破坏你的绑定。我总是尝试在构造函数中初始化绑定到视图的集合。



另外,我喜欢BindableList因为你可以关闭引发事件工作转回来事件并重置绑定。我发现它也适用于大型数据集。



希望这有帮助!

can break your binding. I always try to intialize collections bound to the view only one time in the constructor.

Also, I like BindableList because you can turn off raise events do work turn back on the events and reset the bindings. I find it works well with large datasets as well.

Hope this helps!


这是我发现的关于 BindableCollection 上课时间:

SynchronizedObservableCollection和BindableCollection |原子光照 [ ^ ]

This is what I found about the BindableCollection class on:
SynchronizedObservableCollection and BindableCollection | Atomic Blography[^]
引用:

最常见的是它会被一个ObservableCollection

most commonly it will be fed an ObservableCollection

所以看来你需要使用 ObservableCollection


这篇关于将更改的集合绑定到组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆