订阅事件未触发

时间:2018-05-26 17:28:25

标签: c# wpf events mvvm mvvm-light

我有一个WPF用户控件,这里是代码,我在其中初始化视图模型,并为事件订阅

   public partial class MyUserControl : UserControl
   {
    public MyUserControl()
    {
        InitializeComponent();
        this.DataContext = new MyUserControlViewModel();
        ((MyUserControlViewModel)this.DataContext).MainModel.MessageDataNew.CollectionChanged += NewMessage_CollectionChanged;

    }

这是集合更改事件,它没有触发:(

   private void NewMessage_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        if (MessageStatus != null)
        {
            var border = (Border)VisualTreeHelper.GetChild(MessageStatus, 0);
            var scrollViewer = (ScrollViewer)VisualTreeHelper.GetChild(border, 0);
            scrollViewer.ScrollToBottom();
        }
    }

这是我的视图模型构造函数。我正在使用GalaSoft.MvvmLight.Messaging

  public class MyUserControlViewModel: INotifyPropertyChanged
   {
     public MyUserControlViewModel()
     {        
        Messenger.Default.Register<string>(this, "SimulatorLogs", AddtoCollection);
     }

      public MainModel MainModel
      {
         get { return _mainModel; }
         set
         {   _mainModel = value;
             RaisePropertyChanged(() => MainModel);
         }
    }

    private void AddtoCollection(string measurementData)
    {
        MainModel.MessageDataNew.Add(measurementData);
    }
  }

0 个答案:

没有答案