locked
ViewModel to View interaction using OnPropertyChange() RRS feed

  • Question

  • I am developing painting application in which i need to save my painting.
    To save i need to show save file dialog , As i am implementing MVVM pattern i can't directly use event handler.I heard lot about Mediator and Messenger patterns.
        But while implementing i thought of using PropertyChanged event.
    I have implemented  INotifyPropertyChanged in ViewModel , I have bind all commands. 
    In save command in ViewModel i have called  
            OnPropertyChanged("Show Save Dialog"); // in ViewModel
    and in code behind of user control I have added event handler as
            ViewModel.PropertyChanged += new // in code behind of user control
                  System.ComponentModel.PropertyChangedEventHandler(ViewModel_PropertyChanged);
    and in ViewModel_PropertyChanged i have 
           switch (e.PropertyName ) // in code behind of user control
                {
                    case "Show Save Dialog": ShowSaveFileDialog();// this function shows dialog.
                    break;
                 }
    This works fine in my situation but I don't know the dark side of this implementation.
    Is is right ????

    Wajeed
    Tuesday, August 9, 2011 1:50 PM

Answers

  • As @koshdim put it, it is not good practice to use the PropertyChanged event for this type of behavior.  That is not the purpose of the PropertyChanged event.  It is recommended that you create an event that is specific to this purpose.
    • Marked as answer by wajeed Thursday, August 11, 2011 4:14 AM
    Wednesday, August 10, 2011 1:24 PM

All replies

  • It is okay to use event hanlders as you have.  I would recommend not using the PropertyChanged event, but rather create a dedicated event to show the dialog. Maybe call it Save.  Then your View will simply add a handler for the Save event instead.

    Tuesday, August 9, 2011 4:11 PM
  • Why shouldn't we use PropertyChanged event ? Is there any harm or performance issue , It will be great if u give me some reason so that i can take decisions base on it.
    Wajeed
    Wednesday, August 10, 2011 5:25 AM
  • it is a bad practice to use such code:
    case "Show Save Dialog":
    if you change this string in view model, you will get no compile error but this logic will stop working. it is very hard to debug such bugs.
    using custom event for this would make life easier for one who will read you code (it can be even you after month when you forget about details), so I agree with Brian to use custom event.
    but if you insist to use PropertyChanged I would recommend to declare
    static public string ShowSaveDialogParameter="Show Save Dialog";
    in your view model, then compare it in codebehind as
    if(e.PropertyName == ViewModel.ShowSaveDialogParameter)
    ShowSaveFileDialog();
    it is a little bit safer
    Wednesday, August 10, 2011 9:28 AM
  • As @koshdim put it, it is not good practice to use the PropertyChanged event for this type of behavior.  That is not the purpose of the PropertyChanged event.  It is recommended that you create an event that is specific to this purpose.
    • Marked as answer by wajeed Thursday, August 11, 2011 4:14 AM
    Wednesday, August 10, 2011 1:24 PM