简体   繁体   English

显示自我跟踪实体列表,按状态过滤它们

[英]Displaying list of self-tracking entities, filtering them by state

I want to display a list of self-tracking entities on the form, using standard binding mechanism. 我想使用标准绑定机制在表单上显示自跟踪entities的列表。

What I want is to select entites from list, change/add or remove them, then either submit changes or reject them depending on user choice. 我想要的是从列表中选择实体,更改/添加或删除它们,然后根据用户选择提交更改或拒绝它们。

As far as I understand, for that the list of entities should be attached to the context, then an accept changes call should be made (or not, if the changes are to be rejected). 据我了解,对于entities列表应该附加到上下文的情况,则应该进行一个接受更改调用(或者,如果要拒绝更改,则不进行)。 That means that deleted entities must be in the list as well, marked as deleted. 这意味着删除的实体也必须在列表中,并标记为已删除。

However, they shouldn't be displayed anymore. 但是,不应再显示它们。 That means display should be filtered by the state of entities. 这意味着显示应按实体状态过滤。

Now, usually to do filtering I will wrap my list in BindingList and use the Filter property. 现在,通常要进行过滤,我将列表包装在BindingList并使用Filter属性。 From the other hand, entites do not immediately expose their sate (unlike typed data rows) and hold them in ChangeTracker.State . 另一方面,实体不会立即暴露其状态(与键入的数据行不同),而不会将它们保存在ChangeTracker.State I am at loss, how to do the filtering in this case, especially considering state is an enum, not a plain type. 我很茫然,在这种情况下如何进行过滤,尤其是考虑到状态是一个枚举,而不是普通类型。

[Edit : removed first solution consisting in deleting entities from list] [编辑:删除了从列表中删除实体组成的第一个解决方案]

You may add a IsDeleted Property to your entities 您可以将IsDeleted属性添加到您的实体

public bool IsDeleted
{ 
    get {
        return ChangeTracker.State == ObjectState.Deleted
    } 
}

and filter on that property. 并对该属性进行过滤。 STE are partial classes and may be extended STE是局部类,可以扩展

After some deliberation, I found that BindingList does not support filtering, and the same is technically true for BindingSource . 经过深思熟虑,我发现BindingList不支持过滤,从技术上讲, BindingSource也是如此。

So, with standard filtering out of question, I wrote my own implementation, like this: 因此,毫无疑问地使用标准过滤,我编写了自己的实现,如下所示:

private class LivingBindingList : BindingList<Producer>
{
    public LivingBindingList(List<Producer> source)
        : base(source.Where(producer => producer.ChangeTracker.State != ObjectState.Deleted).ToList())
    {
        rem_cache = source.Where(producer => producer.ChangeTracker.State == ObjectState.Deleted).ToList();
    }
    List<Producer> rem_cache;
    protected override void RemoveItem(int index)
    {
        this.Items[index].MarkAsDeleted();
        this.rem_cache.Add(this.Items[index]);
        base.RemoveItem(index);
    }
    protected override void OnAddingNew(AddingNewEventArgs e)
    {
        e.NewObject = new Producer()
        {
            NameProducer = "Новый производитель",
            GUID = Guid.NewGuid(),
            Type = 1,
            Note = String.Empty
        };
        base.OnAddingNew(e);
    }
    internal IEnumerable<Producer> GetAllForSubmit()
    {
        return this.Items.Concat(rem_cache);
    }
}

With this, I can bind to the list, add and delete as much as I want, and ofr commit purposes retrieve all records, including dead ones, with GetAllForSubmit(). 这样,我可以绑定到列表,根据需要添加和删除任意数量,并且出于提交目的,可以使用GetAllForSubmit()检索所有记录,包括失效的记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM