如何将自定义ObservableCollection元素复制到另一个自定义ObservableCollection?

我创建了一个MTObservableCollection类,该类从ObservableCollection派生用于多线程。该函数的外观如下:

public class MTObservableCollection<T> : ObservableCollection<T> where T: LogClassa
    {
        public MTObservableCollection() { }

        public override event NotifyCollectionChangedEventHandler CollectionChanged;
        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            NotifyCollectionChangedEventHandler CollectionChanged = this.CollectionChanged;
            if (CollectionChanged != null)
                foreach (NotifyCollectionChangedEventHandler nh in CollectionChanged.GetinvocationList())
                {
                    DispatcherObject dispObj = nh.Target as DispatcherObject;
                    if (dispObj != null)
                    {
                        Dispatcher dispatcher = dispObj.Dispatcher;
                        if (dispatcher != null && !dispatcher.Checkaccess())
                        {
                            dispatcher.BeginInvoke(
                                (action)(() => nh.Invoke(this,new NotifyCollectionChangedEventArgs(NotifyCollectionChangedaction.Reset))),DispatcherPriority.DataBind);
                            continue;
                        }
                    }
                    nh.Invoke(this,e);
                }
        }
    }

这是我的LogClass

public class LogClass : INotifyPropertyChanged
{
    private string timestamp;
    private string title;
    private string message;
    private MTObservableCollection<LogClass> childRowLog = new MTObservableCollection<LogClass>();

    public string TimeStamp 
    {
         get { return timestamp; }
         set 
         {
              if( timestamp != value )
              {
                   timestamp = value;
                   OnPropertyChanged("TimeStamp");
              }
         }
    }

    public string Title
    {
         get { return title; }
         set 
         {
              if( title!= value )
              {
                   title= value;
                   OnPropertyChanged("Title");
              }
         }
    }

    public string Message
    {
         get { return message; }
         set 
         {
              if( message!= value )
              {
                   message= value;
                   OnPropertyChanged("Message");
              }
         }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string name)
    {
         PropertyChanged?.Invoke(this,new PropertyChangedEventArgs(name));
    }
}

因此,如果我有两个MTObservableCollection集合,并且向其中之一添加元素,例如:

public static MTObservableCollection<LogClass> parentLogCollection = new MTObservableCollection<LogClass>();
public MTObservableCollection<LogClass> childLogCollection = new MTObservableCollection<LogClass>();

childLogClass.Add( new LogClass { TimeStamp = "time",Title = "title",Message = "message" } );

现在,如果我想将childLogCollection添加到parentLogCollection,我将执行以下操作:

parentLogCollection = new MTObservableCollection<LogClass>(childLogCollection);

我认为我需要在MBObservationCollection类中创建一个实现,

public MTObservableCollection(MTObservableCollection<T> collection)
{

}

我只是不知道要输入什么,我将如何执行?

sbziqian 回答:如何将自定义ObservableCollection元素复制到另一个自定义ObservableCollection?

尝试像这样更改*ngFor="let item of product | slice:0:35;"的父级:

MTObservableCollection
本文链接:https://www.f2er.com/2808086.html

大家都在问