Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
swdev:dotnet:wpf:implementing_mvvm [2010/12/27 10:56]
smayr
swdev:dotnet:wpf:implementing_mvvm [2013/03/12 17:57] (current)
smayr [Resources]
Line 5: Line 5:
   * ViewModel: Models of the data as "viewed" or required by the Views.  A ViewModel encapsulates a Model and exposes it in a format that the View can use.   * ViewModel: Models of the data as "viewed" or required by the Views.  A ViewModel encapsulates a Model and exposes it in a format that the View can use.
   * View: Data binding to ViewModel, with little "code-behind" to implement. It displays a limited window into the Model (data).   * View: Data binding to ViewModel, with little "code-behind" to implement. It displays a limited window into the Model (data).
 +
 +=== Model ===
 +
 +=== View Model ===
 +
 +<code csharp>
 +//========================================================================
 +// Class TAppViewModel
 +//========================================================================
 +public class TAppViewModel : INotifyPropertyChanged
 +{
 +    #region Fields, Events, and Properties
 +    //------------------------------------------------------------------------
 +    // Fields
 +    //------------------------------------------------------------------------
 +    private string m_ApplicationName        = "MyApp";
 +    private string m_ApplicationVersion     = "5.0";
 +    private string m_ApplicationBuildNumber = "20110310";
 +    private string m_ApplicationDevStage    = "Dev";
 +    private int m_Tag            = 0;
 +
 +    //------------------------------
 +    // Events
 +    //------------------------------
 +    public event PropertyChangedEventHandler PropertyChanged;
 +    
 +    //------------------------------------------------------------------------
 +    // Properties
 +    //------------------------------------------------------------------------
 +    public string ApplicationName
 +    {
 +        get { return m_ApplicationName; }
 +        set { m_ApplicationName = value; }
 +    }
 +    public string ApplicationVersion
 +    {
 +        get { return m_ApplicationVersion; }
 +        set { m_ApplicationVersion = value; }
 +    }
 +    public string ApplicationBuildNumber
 +    {
 +        get { return m_ApplicationBuildNumber; }
 +        set { m_ApplicationBuildNumber = value; }
 +    }
 +    ...
 +    
 +    public TAppViewModel()
 +    {
 +    }
 +    
 +    public void SomeWorkToDo()
 +    {
 +       // Trigger Change
 +       if (PropertyChanged != null)
 +       {
 +                TAppLog.LogMessage("TFittingVM.SetBusyFlag", "PropertyChanged event triggered for: IsDataLoading, IsControlAvailable.");
 +                PropertyChanged(this, new PropertyChangedEventArgs("IsDataLoading"));
 +                PropertyChanged(this, new PropertyChangedEventArgs("IsControlAvailable"));
 +       }
 +    }
 +    
 +}    
 +</code>
 +
 === Main View === === Main View ===
 The ''Main View'' should implement an instance of ViewModel as a ObjectDataProvider (in XAML), that we call ''AppViewModel''. The ''Main View'' should implement an instance of ViewModel as a ObjectDataProvider (in XAML), that we call ''AppViewModel''.
Line 27: Line 91:
 </<UserControl.Resources> </<UserControl.Resources>
 </code> </code>
-=== User Controls === + 
-User Controls in ''Main View'' should implement an ''AppViewModel'' dependency property. For example, user controls should be called as:+=== User Controls (View) === 
 + 
 +User Controls in ''Main View'' should implement an ''AppViewModel'' dependency property.  
 + 
 +For example, in the ''Main View'', user controls instances should be called as:
 <code xml> <code xml>
 <TabControl> <TabControl>
Line 49: Line 117:
 </code> </code>
  
-The dependency property ''AppViewModel'' would be implemented like this:+The user control dependency property ''AppViewModel'' would be implemented like this (eg, in user control ''ucUserControlTriangle ''):
 <code csharp> <code csharp>
 public class ucUserControlTriangle : UserControl public class ucUserControlTriangle : UserControl
 { {
 +  #region AppViewModel dependency property implementation
   ///-------------------------------   ///-------------------------------
-  /// Dependency Properties+  /// App View Model
   ///-------------------------------   ///-------------------------------
  
Line 79: Line 148:
   set { SetValue(AppViewModelProperty, value); }   set { SetValue(AppViewModelProperty, value); }
   }   }
- + 
-  ///------------------------------- +
-  /// Constructor  +
-  ///------------------------------- +
-  public ucUserControlTriangle() +
-  { +
-    InitializeComponents(); +
-   +
-    // Set DataContext +
-    this.DataContext = AppViewModel; +
-  } +
-  +
   ///----------------------------------------------------------------------------------------   ///----------------------------------------------------------------------------------------
   /// <summary>   /// <summary>
Line 138: Line 196:
   }   }
      
 +  #endregion
 +  
 +  ...
 +  
 +  ///-------------------------------
 +  /// Constructor 
 +  ///-------------------------------
 +  public ucUserControlTriangle()
 +  {
 +    InitializeComponents();
 +  
 +    // Set DataContext
 +    this.DataContext = AppViewModel;
 +  }
 +
   ...   ...
      
Line 176: Line 249:
 </UserControl> </UserControl>
 </code> </code>
 +
 +== Resources ==
 +  * [[http://wpftutorial.net/MVVM.html|WPF Tutorial: MVVM (Overview)]]
 +  * [[http://msdn.microsoft.com/en-us/magazine/dd419663.aspx|MSDN Magazine: WPF Apps With The Model-View-ViewModel Design Pattern, by Josh Smith]]
 +  * [[http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx|Simplifying the WPF TreeView by Using the ViewModel Pattern, by Josh Smith]]
 +  * [[http://stackoverflow.com/questions/1798600/mvvm-what-is-the-ideal-way-for-usercontrols-to-talk-to-each-other|MVVM: What is the ideal way for usercontrols to talk to each other]]
 +  * [[http://lostechies.com/gabrielschenker/2009/05/31/fluent-silverlight-part-1/\|Los Techies: Fluent Silverlight (using MVVM)]]
 +  * [[http://msdn.microsoft.com/en-us/library/ms752308.aspx|MSDN: Commanding Overview]]
 +  * [[http://msdn.microsoft.com/en-us/magazine/cc785480.aspx|MSDN Magazine: Understanding Routed Events and Commands In WPF, by Brian Noyes]]
 +  * [[http://zamjad.wordpress.com/2009/07/13/dependency-property-metadata|Dependency Property Metadata]]
 +
 +