I have been reading about MVVM (and MVVM Light) lately, so tried to implement in an application with 2 ViewModels.
When I use the ViewModelLocator in the datacontext the Command binding does not work, if I bind the ViewModel to the datacontext of the ViewModel itself it works!
What am I missing here?
public class ViewModelLocator
{
public ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
SimpleIoc.Default.Register<MotionViewModel>();
SimpleIoc.Default.Register<LiveViewViewModel>();
}
public LiveViewViewModel liveViewViewModel
{
get
{
return ServiceLocator.Current.GetInstance<LiveViewViewModel>();
}
}
public MotionViewModel motionViewModel
{
get
{
return ServiceLocator.Current.GetInstance<MotionViewModel>();
}
}
public static void Cleanup()
{
ClearLiveViewViewModel();
ClearMotionViewModel();
}
public static void ClearLiveViewViewModel()
{
ServiceLocator.Current.GetInstance<LiveViewViewModel>().CloseCamera();
ServiceLocator.Current.GetInstance<LiveViewViewModel>().Cleanup();
}
public static void ClearMotionViewModel()
{
ServiceLocator.Current.GetInstance<MotionViewModel>().Cleanup();
}
}
This is the ViewModel code:
public class MotionViewModel : ViewModelBase
{
private RelayCommand _mocoConnectCommand;
public MotionViewModel()
{
if (IsInDesignMode)
{
// Code runs in Blend --> create design time data.
}
else
{
// Code runs "for real"
Task.Factory.StartNew(() => Initialize());
}
}
public RelayCommand MoCoConnectCommand
{
get;
private set;
}
private void Initialize()
{
MoCoConnectCommand = new RelayCommand(MoCoConnect);
}
private void MoCoConnect()
{
MessageBox.Show("Connection button pressed");
}
#endregion
}
This is the XAML View Code:
<UserControl.Resources>
<ResourceDictionary>
<vm:ViewModelLocator x:Key="Locator"/>
</ResourceDictionary>
</UserControl.Resources>
<UserControl.DataContext>
<Binding Source="{StaticResource Locator}" />
</UserControl.DataContext>
<Button Style="{DynamicResource MahApps.Metro.Styles.MetroButton}"
Grid.Column="0" Grid.Row="0"
Height="30" Width="28" Margin="-1,2,1.333,2.667"
Command="{Binding MoCoConnectCommand}" >
<iconPacks:FontAwesome Kind="LinkSolid"/>
</Button>
The user control's data context is a
ViewModelLocator
and the button is binding toMoCoConnectCommand
property. But the classViewModelLocator
don't have the propertyMoCoConnectCommand
.I think you need inject
MotionViewModel
in the user control's data context, like :