【问题标题】:How to copy custom ObservableCollection elements to another custom ObservableCollection?如何将自定义 ObservableCollection 元素复制到另一个自定义 ObservableCollection?
【发布时间】:2020-01-07 21:34:37
【问题描述】:

我为多线程创建了一个派生自 ObservableCollection 的 MTObservableCollection 类。函数如下所示:

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

这是我的日志类

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)
{

}

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

【问题讨论】:

    标签: c# wpf list observablecollection


    【解决方案1】:

    尝试像这样更改 MTObservableCollection 的父级:

    public MTObservableCollection(MTObservableCollection<T> collection) : base(collection)
    {
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多