{"id":62,"date":"2011-02-02T16:53:00","date_gmt":"2011-02-02T16:53:00","guid":{"rendered":"https:\/\/wdev-blog.azurewebsites.net\/index.php\/2011\/02\/02\/mvvm-basic-principals\/"},"modified":"2011-02-02T16:53:00","modified_gmt":"2011-02-02T16:53:00","slug":"mvvm-basic-principals","status":"publish","type":"post","link":"http:\/\/panahy.nl\/index.php\/2011\/02\/02\/mvvm-basic-principals\/","title":{"rendered":"MVVM Basic Principals"},"content":{"rendered":"<div>\n<p><span>The very basic principals so far I understand from M-V-VM practice is the following which<\/span> is understood from this article : <a href=\"http:\/\/msdn.microsoft.com\/en-us\/magazine\/dd419663.aspx#id0090025\">http:\/\/msdn.microsoft.com\/en-us\/magazine\/dd419663.aspx#id0090025<\/a><\/p>\n<p><\/p>\n<h3>View is a Control or Window<\/h3>\n<pre><span style=\"color: #0000FF;\">public<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">partial<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">class<\/span><span style=\"color: #000000;\"> CustomerView : System.Windows.Controls.UserControl<br \/>{<br \/><\/span><span style=\"color: #0000FF;\">public<\/span><span style=\"color: #000000;\"> CustomerView()<br \/>{<br \/>InitializeComponent();<br \/>}<br \/>}<\/span><\/pre>\n<p>And this is as far as the view might be written in code. In the most cases we don&#8217;t want to write more code in the View. The reason is that the View is supposed to be made loosely coupled from the data and the logic, probably done by creative designers.<\/p>\n<h3>The View gets DataContext<\/h3>\n<p>Some magic forces will get the data that is accessible through the ViewModel and set it to the DataContext property of the control:<\/p>\n<pre><span style=\"color: #0000FF;\">protected<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">override<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">void<\/span><span style=\"color: #000000;\"> OnStartup(StartupEventArgs e)<br \/>{<br \/><\/span><span style=\"color: #0000FF;\">base<\/span><span style=\"color: #000000;\">.OnStartup(e);<br \/><br \/>var viewModel = <\/span><span style=\"color: #0000FF;\">new<\/span><span style=\"color: #000000;\"> MainWindowViewModel(path);<br \/>MainWindow window = <\/span><span style=\"color: #0000FF;\">new<\/span><span style=\"color: #000000;\"> MainWindow();<br \/>window.<b>DataContext <\/b>= viewModel;<br \/><br \/>window.Show();<br \/>}<br \/><\/span><\/pre>\n<p>In this example the <i>magic<\/i> happens in the Startup method of the application. Using <a href=\"http:\/\/mef.codeplex.com\/\" title=\"Managed Extensibility Framework \">MEF<\/a> this magic is taken out to some sophisticated standard way.<br \/>Now the View and its elements are ready to consume the data.<\/p>\n<h3>View binds to ViewModel<\/h3>\n<p>Both data and actions are binded to public accessors of ViewModel:<\/p>\n<pre><span style=\"color: #000000;\"> &lt;TextBox<br \/>    x:Name=<\/span><span style=\"color: #800000;\">\"firstNameTxt\"<\/span><span style=\"color: #000000;\"><br \/>    Grid.Row=<\/span><span style=\"color: #800000;\">\"2\"<\/span><span style=\"color: #000000;\"> Grid.Column=<\/span><span style=\"color: #800000;\">\"2\"<\/span><span style=\"color: #000000;\"><br \/>    Text=<\/span><span style=\"color: #800000;\">\"{Binding Path=FirstName, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}\"<\/span><span style=\"color: #000000;\"><br \/>    Validation.ErrorTemplate=<\/span><span style=\"color: #800000;\">\"{x:Null}\"<\/span><span style=\"color: #000000;\"><br \/>    \/&gt;<\/span><br \/><br \/><span style=\"color: #000000;\">&lt;Button <br \/>  Command=<\/span><span style=\"color: #800000;\">\"{Binding Path=SaveCommand}\"<\/span><span style=\"color: #000000;\"><br \/>  Content=<\/span><span style=\"color: #800000;\">\"_Save\"<\/span><span style=\"color: #000000;\"><br \/>  \/&gt;<\/span><br \/><\/pre>\n<h3>ViewModel is unaware of View<\/h3>\n<p>Unlike the Presenter in MVP, a ViewModel does not need a reference to a view. This makes it possible to attach any View at any time to consume the public data provided by ViewModel.<\/p>\n<p>ViewModel exposes public properties to share data with any possible View:<\/p>\n<pre><span style=\"color: #0000FF;\">public<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">class<\/span><span style=\"color: #000000;\"> CustomerViewModel : WorkspaceViewModel, IDataErrorInfo<br \/>{<br \/>    <\/span><span style=\"color: #0000FF;\">public<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">string<\/span><span style=\"color: #000000;\"> FirstName<br \/>    {<br \/>        <\/span><span style=\"color: #0000FF;\">get<\/span><span style=\"color: #000000;\"> { <\/span><span style=\"color: #0000FF;\">return<\/span><span style=\"color: #000000;\"> _customer.FirstName; }<br \/>        <\/span><span style=\"color: #0000FF;\">set<\/span><span style=\"color: #000000;\"><br \/>        {<br \/>            <\/span><span style=\"color: #0000FF;\">if<\/span><span style=\"color: #000000;\"> (value == _customer.FirstName)<br \/>                <\/span><span style=\"color: #0000FF;\">return<\/span><span style=\"color: #000000;\">;<br \/><br \/>            _customer.FirstName = value;<br \/><br \/>            <\/span><span style=\"color: #0000FF;\">base<\/span><span style=\"color: #000000;\">.OnPropertyChanged(<\/span><span style=\"color: #800000;\">\"FirstName\"<\/span><span style=\"color: #000000;\">);<br \/>        }<br \/>    }<br \/><\/span><\/pre>\n<p>ViewModel exposes public commands to share actions with any possible View:<\/p>\n<pre><span style=\"color: #808080;\">\/\/\/<\/span><span style=\"color: #008000;\"> <\/span><span style=\"color: #808080;\">&lt;summary&gt;<\/span><span style=\"color: #008000;\"><br \/><\/span><span style=\"color: #808080;\">\/\/\/<\/span><span style=\"color: #008000;\"> Returns a command that saves the customer.<br \/><\/span><span style=\"color: #808080;\">\/\/\/<\/span><span style=\"color: #008000;\"> <\/span><span style=\"color: #808080;\">&lt;\/summary&gt;<\/span><span style=\"color: #008000;\"><br \/><\/span><span style=\"color: #0000FF;\">public<\/span><span style=\"color: #000000;\"> ICommand SaveCommand<br \/>{<br \/>    <\/span><span style=\"color: #0000FF;\">get<\/span><span style=\"color: #000000;\"><br \/>    {<br \/>        <\/span><span style=\"color: #0000FF;\">if<\/span><span style=\"color: #000000;\"> (_saveCommand == <\/span><span style=\"color: #0000FF;\">null<\/span><span style=\"color: #000000;\">)<br \/>        {<br \/>            _saveCommand = <\/span><span style=\"color: #0000FF;\">new<\/span><span style=\"color: #000000;\"> RelayCommand(<br \/>                param =&gt; <\/span><span style=\"color: #0000FF;\">this<\/span><span style=\"color: #000000;\">.Save(),<br \/>                param =&gt; <\/span><span style=\"color: #0000FF;\">this<\/span><span style=\"color: #000000;\">.CanSave<br \/>                );<br \/>        }<br \/>        <\/span><span style=\"color: #0000FF;\">return<\/span><span style=\"color: #000000;\"> _saveCommand;<br \/>    }<br \/>}<\/span><\/pre>\n<p>Some of these properties may be data coming from a model and some might be simply a state of the ViewModel to help out the View. <\/p>\n<h3>Data Manipulation<\/h3>\n<p>The ViewModel, never the View, performs all modifications made to the model data.<br \/>As I already mentioned the ViewModel is unaware of View but it is certainly aware of Model and can manipulate it when needed. When user interacts with the View, the binding manager will take care of both ways of data modifications in each binding.<\/p>\n<p>The way a ViewModel is attached to a model, is an architectural decision and makes your application easier or hard to understand and maintain. But this is another important subject. For now, like MEF does the magic for coupling View and ViewModel, there needs to be some Factory or Controller mechanism to pull the data and assign it to the ViewModel.<\/div>\n","protected":false},"excerpt":{"rendered":"<p>The very basic principals so far I understand from M-V-VM practice is the following which is understood from this article : http:\/\/msdn.microsoft.com\/en-us\/magazine\/dd419663.aspx#id0090025 View is a Control or Window public partial class CustomerView : System.Windows.Controls.UserControl{public CustomerView(){InitializeComponent();}} And this is as far as the view might be written in code. In the most cases we don&#8217;t want &hellip; <a href=\"http:\/\/panahy.nl\/index.php\/2011\/02\/02\/mvvm-basic-principals\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;MVVM Basic Principals&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[58],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"post-thumbnail":false},"uagb_author_info":{"display_name":"Pouya Panahy","author_link":"http:\/\/panahy.nl\/index.php\/author\/pouya\/"},"uagb_comment_info":0,"uagb_excerpt":"The very basic principals so far I understand from M-V-VM practice is the following which is understood from this article : http:\/\/msdn.microsoft.com\/en-us\/magazine\/dd419663.aspx#id0090025 View is a Control or Window public partial class CustomerView : System.Windows.Controls.UserControl{public CustomerView(){InitializeComponent();}} And this is as far as the view might be written in code. In the most cases we don&#8217;t want&hellip;","_links":{"self":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/62"}],"collection":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/comments?post=62"}],"version-history":[{"count":0,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/62\/revisions"}],"wp:attachment":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/media?parent=62"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/categories?post=62"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/tags?post=62"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}