繁体   English   中英

未从列表中删除项目

[英]Item is not being remove from list

未从列表中删除项目

这是我的代码:

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; }
    }
}

在主要

按钮单击项未删除。 请建议我我做错了什么。

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

请建议我正在这段代码中做。

谢谢阿米特

您正在创建一个新的空列表,然后尝试删除位置1的元素。实际上,您刚刚覆盖了原始列表。

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

我注释掉的行会创建一个新列表并在尝试删除该项目之前立即覆盖 a.Connections 这就是导致代码失败的原因。

似乎在删除连接之前,您正在替换连接列表。

由于您已将其标记为WPF,因此我将假定您能够从列表中删除该项目,但该项目仍显示在屏幕上。 尝试这个:

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

或者,您可以使用ObservableCollection<IEmpConnection> 这是一个特殊的集合,当集合更改时会引发事件。 然后,您将简单地删除对象,然后屏幕将更新。

暂无
暂无

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

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