ICommand CanExecute not triggering after PropertyChanged?

The ICommand interface exposes an event ICommand.CanExecuteChanged which is used to inform the UI when to re-determine the IsEnabled state of command driven UI components. Depending upon the implementation of the RelayCommand you are using, you may need to raise this event; Many implementations expose a method such as RelayCommand.RaiseCanExecuteChanged() which you can invoke to … Read more

how to use MVVMLight SimpleIoc? [closed]

SimpleIoc crib sheet: You register all your interfaces and objects in the ViewModelLocator class ViewModelLocator { static ViewModelLocator() { ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default); if (ViewModelBase.IsInDesignModeStatic) { SimpleIoc.Default.Register<IDataService, Design.DesignDataService>(); } else { SimpleIoc.Default.Register<IDataService, DataService>(); } SimpleIoc.Default.Register<MainViewModel>(); SimpleIoc.Default.Register<SecondViewModel>(); } public MainViewModel Main { get { return ServiceLocator.Current.GetInstance<MainViewModel>(); } } } Every object is a singleton by default. To … Read more

What is a ViewModelLocator and what are its pros/cons compared to DataTemplates?

Intro In MVVM the usual practice is to have the Views find their ViewModels by resolving them from a dependency injection (DI) container. This happens automatically when the container is asked to provide (resolve) an instance of the View class. The container injects the ViewModel into the View by calling a constructor of the View … Read more

What is the template binding vs binding?

TemplateBinding is used for binding to the element properties within the template definition. In your example, you could have written: <Border Padding=”{Binding Padding}” …> …meaning to bind the border’s padding property to the padding property of… what? You’d like to say, “padding property of the control that this template is being used for.” You can’t … Read more

Handling the window closing event with WPF / MVVM Light Toolkit

I would simply associate the handler in the View constructor: MyWindow() { // Set up ViewModel, assign to DataContext etc. Closing += viewModel.OnWindowClosing; } Then add the handler to the ViewModel: using System.ComponentModel; public void OnWindowClosing(object sender, CancelEventArgs e) { // Handle closing logic, set e.Cancel as needed } In this case, you gain exactly … Read more