0

I am new to MVVM. In my code behind for the view I have a lot of code in the window initialized event. It has delegates to handle several events. I want to take of all this in the view model. Can we have delegates to handle events in the ViewModel when you are implementing MVVM using MVVM Light? I tried to grasp EventToCommand mechanism, but its hard to understand. Can someone use this code example to help me understand how?

private void Window_Initialized(object sender, EventArgs e)
  {
     try
     {
        _AppComment = new CommentHandler(App_Comment);
        _AppUnitComment = new UnitCommentHandler(App_UnitComment);
        _AppActualListChanged = new System.Collections.Specialized.NotifyCollectionChangedEventHandler(ActualList_CollectionChanged);

        DataContext = AppVerifier.Instance;
        AppVerifier.Instance.Comment += _AppComment;
        AppVerifier.Instance.UnitComment += _AppUnitComment;
        AppVerifier.Instance.ActualList.CollectionChanged += _AppActualListChanged;
        AppVerifier.Instance.PropertyChanged += App_PropertyChanged;
        AppVerifier.Instance.msgClient.PropertyChanged += App_ConnectivityChanged;
        AppVerifier.Instance.ActualListReceived += App_ActualListReceived;
        PopulateTestMenu();
        PopulateChangeMenu();

        _jumpToLog.Header = "View in Log...";
        Uri uri = new Uri(@"/App;component/Resources/log.png", UriKind.Relative);
        System.Windows.Media.Imaging.BitmapImage bitmap = new System.Windows.Media.Imaging.BitmapImage(uri);
        Image imgIcon = new Image();
        imgIcon.Height = 16;
        imgIcon.Width = 16;
        imgIcon.Source = bitmap;
        _jumpToLog.Icon = imgIcon;
        _jumpToLog.Click += new RoutedEventHandler(JumpToLog_Click);
        _jumpToLog.ToolTip = "Shows the selected feature in the log window.";

        Binding b = new Binding();
        b.Source = listFeatures;
        b.Path = new PropertyPath("SelectedIndex");
        b.Converter = new EnabledConverter();

        menuReset.SetBinding(MenuItem.IsEnabledProperty, b);
        menuBarReset.SetBinding(MenuItem.IsEnabledProperty, b);

        listFeatures.SelectionChanged += new SelectionChangedEventHandler(listFeatures_SelectionChanged);

        MsgQueueWindow.Instance.MsgQueue.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(MsgQueue_CollectionChanged);
        UnitMsgQueueWindow.Instance.MsgQueue.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(UnitMsgQueue_CollectionChanged);

        if (!Settings.Default.MainPos.IsEmpty)
        {
           Left = Settings.Default.MainPos.X;
           Top = Settings.Default.MainPos.Y;
        }

        if (!Settings.Default.MainSize.IsEmpty)
        {
           Height = Settings.Default.MainSize.Height;
           Width = Settings.Default.MainSize.Width;
        }

        AppVerifier.Instance.msgClient.RouterFinder.Connect();
        AppVerifier.Instance.msgClient.RouterFinder.Poll();

        Data.LogHandler.LogSettings(); //7121 Move Logs To AppDataFolder

        if (Settings.Default.AutoConnect)
        {
           System.Net.IPAddress ip;

           if (System.Net.IPAddress.TryParse(Settings.Default.Ip, out ip))
           {
              AppVerifier.Instance.Connect(ip, Settings.Default.Port, Settings.Default.Key);
           }
        }

        if (Settings.Default.AutoStartFileLog)
        {
           string fileName = DateTime.Now.ToString("yyyy-MM-dd HHmmss") + " ApplicationVerification.csv";
           string path = System.IO.Path.Combine(Settings.Default.AutoStartFileLogPath, fileName);
           Logger.Instance.StartFileLogging(path);
        }



     }
     catch (Exception ex)
     {
        ErrorLogger.Log(LogLevel.Error,ex.ToString());
     }                                                                                                                                                                                                                       
  }
Asha
  • 25
  • 1
  • 7
  • Welcome to Stack Overflow. Your question seems to be a duplicate of [What's the best way to pass event to ViewModel?](http://stackoverflow.com/questions/21285577/whats-the-best-way-to-pass-event-to-viewmodel) and so I have voted to close it. However, if you follow the link to that question and see the accepted answer, you will find a solution to your problem. In short, when using MVVM, we can handle event using Attached Properties. – Sheridan Apr 23 '14 at 21:54
  • Found this article very helpful. [link](http://www.freddes.se/2009/12/04/update-views-through-events-between-their-viewmodels/ ) – Asha May 06 '14 at 17:36

0 Answers0