簡體   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