Yes, you can create a static resource in user control
<UserControl x:Class="MvvmLight1.UserControl1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300" xmlns:vm="clr-namespace:MvvmLight1.ViewModel" > <UserControl.Resources> <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" /> </UserControl.Resources> <Grid> </Grid> </UserControl>
but IMO is not a good idea to use MVVM Light ViewModelLocator for UserControles, because it is a static property, and when you are going to instantiate multiple instances of your user control, they will have the same general ViewModel so that they all work the same way and it's not that what we want for UserControl if you decide to use it once in your entire project.
to get around this problem, you need to change the ViewModelLocator to make all Non static properties, for example:
public class ViewModelLocator { // v--- You got to comment this out private /*static*/ MainViewModel _main; public ViewModelLocator() { CreateMain(); } public /*static*/ MainViewModel MainStatic { get { if (_main == null) { CreateMain(); } return _main; } } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "This non-static member is needed for data binding purposes.")] public MainViewModel Main { get { return MainStatic; } } public /*static*/ void ClearMain() { _main.Cleanup(); _main = null; } public /*static*/ void CreateMain() { if (_main == null) { _main = new MainViewModel(); } } public /*static*/ void Cleanup() { ClearMain(); } }
Ehsan Zargar Ershadi
source share