C# – WPF Binding vs Event Handling

bindingcevent handlingwpf

I'm new to WPF and come from a WinForms background and have a fairly basic question about binding vs event handling.

To try and maintain some separation of responsibility I've have a bunch of Presentation objects which simply have Dependency Properties to hold the UI data parts of a business object, the business object contains similar data but the data types are occationally different so that the Presentation object is correct for display purposes. So something like

public class MyPresentation
{
   // bunch of dependency properties
   public bool MyProperty
   {
      get { return (bool)GetValue(MyPropertyProperty); }
      set { SetValue(MyPropertyProperty, value); }
   }

   // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
   public static readonly DependencyProperty MyPropertyProperty =
   DependencyProperty.Register("MyProperty", typeof(bool), typeof(MyPresentationObject), new UIPropertyMetadata(false, MyPresentationObject.MyPropertyPropertyChanged));

   MyBusinessObject RelatedBusinessObject { get; set;}

   public MyPresentation(MyBusinessObject businessObejct)
   {
      this.RelatedBusinessObject = businessObject;
   }


   public static void MyPropertyPropertyChanged()
   {
      // Do some stuff to related business objects
   }
}

The properties of MyPresentation are then data bound to various controls and I use Triggers etc to change presentation dependency properties which causes business object changes in the OnPropertyChanged event

The question I have is am I using binding in the correct fashion? Normally (in Winforms) I'd have used click events etc to change my business objects (or the presentation versions of them) values but those sort of events and that sort of event handling seems superfluous now that you can use Binding, Triggers and OnPropertyChanged events.

Am I missing something?

Best Answer

Look here this present the pattern Model View ViewModel, this permit you to take full advantages on the binding, and Command of WPF, without disturb your business objects (like implementing WPF stuff like INotifyPropertyChanged on your business objects)