Posts Tagged ‘MVVM’

To pratice the MVVM pattern, I’ve refactored the GeometryViz3D tool using CoreMVVM. Several lessons learned:

  • With the MVVM pattern, the code of some Views can be reduced to just a constructor. The plumbing between Views and ViewModels is done through data binding, making it possible to test the system “under the skin”, meaning that we will be able to unit-test the Model and the ViewModels, and the Views are just a thin skin, a XAML file.
  • When binding a property (source) of the ViewModel to the Text property of a TextBox, by default, the source will be updated when the TextBox loses the focus. It is possible to update the source as soon as the Text of the TextBox is changed by specifying the UpdateSourceTrigger property of the Binding to PropertyChanged.
    Text=”{Binding Path=X, UpdateSourceTrigger=PropertyChanged}”
  • In Windows Forms programming, we typically set the initial focus of a Window in the Loaded event. In WPF, we can set the initial focus using the FocusManager. The following example, set the initial focus to the TextBox named tbName.

    FocusManager.FocusedElement=”{Binding ElementName=tbName}”>

Read Full Post »