In the last days I studied MVVM and I downloaded a couple of samples.
The following article is very good and it included the source code:
Better User and Developer Experiences – From Windows Forms to WPF with MVVM
https://web.archive.org/web/20220408112054/http://reedcopsey.com/series/windows-forms-to-mvvm/
When I first opened the project in Visual Studio 2013 all the fields and lists were empty like expected. The program is a RSS reader and I loaded one feed with lots of feed items which filled in all the fields in the form. But now even when I stopped the program I can see all the data in the design.
This is obviously very good because it makes it a lot easier to see and possibly amend the design.
I think what I saw is covered under the name Blendability – but I am not sure about this.
Now my question: Where does this Blendability come from? I looked at the source code of the project and I don’t find where the data comes from in design mode.
Over the last days I installed a couple of extensions in Visual Studio and maybe I installed something which causes this behaviour. Or maybe this is some special function in this example (which is not mentioned anywhere in the article).
If possible please let me know where this Blendability comes from and how I can implement it in my own projects.
Design time has a nifty feature where if you were to set the DataContext
to a class, then that class will actually be instantiated at design-time.
It's quite probable that the DataContext
of this particular window is a class with a constructor that executes the RSS reading code.
Take this for example:
public class MyRssReaderViewModel
{
public MyRssReaderViewModel()
{
//Read RSS and populate properties
LoadRSS();
}
public void LoadRSS()
{
...
}
...
}
Now if you were to set the DataContext
like this:
<Window.DataContext>
<ViewModels:MyRssReaderViewModel/>
</Window.DataContex>
The designer will instantiate the class at design-time, and by extension call the LoadRSS
method. Any elements that reference properties in your view model will update to display the bound data.
One important thing to note is that the class will be instantiated every time you build the project.