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 [2011/03/09 14:35]
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 185: 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]]
 +
 +