CommunityToolkit / CommunityToolkit/MVVM-Samples
Usage of view models in XAML Designer in Visual Studio or Blend
- Dominant language
- No language data
- Stars
- 1.4k
- Forks
- 265
- PR merge metrics
- No merged PRs in 30d
Description
### Ask:
It would be great to see a recommended approach for
- how to use the view models in the XAML Designer (in Visual Studio or Blend).
- best practices around switching between real view models/ services and view models containing sample data
### Rationale:
The XAML Designer does not automatically load C# code. E.g. if you register your viewmodels with dependency injection (which you make available through a property on `App` and in your codebehind you write `this.DataContext = ((App)Application.Current).Services.GetService();` then the XAML designer will not be able to load it.
The Blend / the XAML designer can only load objects with a default (parameterless) constructor that are initialized in XAML itself, e.g.
```xml
```
One suggested solution to this (which MVVMLight did) was the use of the ViewModelLocator pattern - which you could include in your XAML and which would construct the viewmodel for you.
### Some relevant links:
- https://docs.microsoft.com/en-us/windows/uwp/data-binding/displaying-data-in-the-designer
- https://stackoverflow.com/questions/5462040/what-is-a-viewmodellocator-and-what-are-its-pros-cons-compared-to-datatemplates
- https://stackoverflow.com/questions/29399157/how-do-i-specify-datacontext-viewmodel-type-to-get-design-time-binding-checkin/29399984
- https://stackoverflow.com/questions/25366291/how-to-handle-dependency-injection-in-a-wpf-mvvm-application
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.