简体   繁体   English

未从列表中删除项目

[英]Item is not being remove from list

Item is not being remove from list 未从列表中删除项目

here is my code: 这是我的代码:

public interface IEmpConnection
{
    int SegId { get; set; }
}

public class EmpConnection : IEmpConnection
{

    private int segid;
    public int SegId
    {
        get
        {
            return segid;
        }
        set
        {
            segid = value;
        }
    }
}

public class CustomerConnection : EmpConnection, ICustomerConnection
{

    private int _id;

    public int Id
    {
        get
        {
            return _id;
        }
        set
        {
            _id = value;
        }
    }
}

public interface ICustomerConnection
{
    int Id { get; set; }


}

public class CustConn : CustomerConnection
{
    private ObservableCollection<CustomerConnection> _airSeg;

    public CustConn()
    {
        _airSeg = new System.Collections.ObjectModel.ObservableCollection<CustomerConnection>();
        _airSeg.Add(new AirSegmentConnection { Id = 1, SegId = 2 });
        _airSeg.Add(new AirSegmentConnection { Id = 1, SegId = 3 });
    }

    private bool isDeleted;

    public bool IsDeleted
    {
        get { return isDeleted; }
        set { isDeleted = value; }
    }

    private List<IEmpConnection> _connection;
    public List<IEmpConnection> Connections
    {
        get
        {
            var s = new AirSegmentConnection();
            var k = s as ISegmentConnection;

            if (IsDeleted)
            {
                _airSeg.RemoveAt(1);
            }

            return _connection = _airSeg.ToList().Cast<ISegmentConnection>().ToList();
            //return _airSeg.ToList().Cast<ISegmentConnection>().ToList();
        }

        set
        {
            _connection = value;
            //_airSeg = new System.Collections.ObjectModel.ObservableCollection<ISegmentConnection>(value.ToList()) ;
        }
    }

    private ObservableCollection<CustomerConnection> airConnection;

    public ObservableCollection<CustomerConnection> AirConnection
    {
        get { return _airSeg; }
        set { _airSeg = value; }
    }
}

on main 在主要

button click item is not being removed. 按钮单击项未删除。 please suggest me where i am doing wrong. 请建议我我做错了什么。

CustConn a = new CustConn();
if (a.Connections.Count > 0)
{
    a.Connections = new List<IEmpConnection>();
    a.Connections.RemoveAt(1);// this item is not being removed.
}

please suggest i am doing worng in this code. 请建议我正在这段代码中做。

Thanks Amit 谢谢阿米特

You're creating a new, empty list and then trying to remove an element at position 1. In fact you've just overwritten your original list. 您正在创建一个新的空列表,然后尝试删除位置1的元素。实际上,您刚刚覆盖了原始列表。

if (a.Connections.Count > 0)
{
    /// REMOVE THIS LINE a.Connections = new List<IEmpConnection>();
    a.Connections.RemoveAt(1);// this item is not being removed.
}

The line I've commented out it creating a new list and overwriting a.Connections immediately before you try to remove the item. 我注释掉的行会创建一个新列表并在尝试删除该项目之前立即覆盖 a.Connections This is what's causing the code to fail. 这就是导致代码失败的原因。

It seems that you are replacing the list of connections before you remove the connection. 似乎在删除连接之前,您正在替换连接列表。

Since you have this marked as WPF, I'm going to assume that at some point you were able to remove the item from the List, but it still appeared on the screen. 由于您已将其标记为WPF,因此我将假定您能够从列表中删除该项目,但该项目仍显示在屏幕上。 Try this: 尝试这个:

if (a.Connections.Count > 0)
{
     var newList = new List<IEmpConnection>(a.Connections);
     a.Connections.RemoveAt(1);
     a.Connections = newList;
} 

Alternatively, you may be able to use an ObservableCollection<IEmpConnection> . 或者,您可以使用ObservableCollection<IEmpConnection> This is a special collection that raises events when the collection changes. 这是一个特殊的集合,当集合更改时会引发事件。 Then you would simple remove the object, and the screen would update. 然后,您将简单地删除对象,然后屏幕将更新。

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

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