Historically in WPF when using MVVM (typically done with MVVMLight) I use the approach where I bind a control to something in its view-model. To wit:
XAML:
xmlns:vm="clr-namespace:My.Namespace.ViewModels"
And then declare the data context in XAML using
DataContext="{Binding Main, Source={StaticResource Locator}}"
ViewModel:
public class MainViewModel : BaseViewModel
{
// Properties, methods, commands etc. here
}
MVVMLight kicked in and provides the ViewModel locator which allowed me to do the following in the App.xaml
<local:ViewModelLocator x:Key="locator"/>
local
is defined earlier in the XAML similar to how
My questions are:
-
What’s the way to follow this kind of pattern now with MVVM Toolkit? The Migrating from MvvmLight article glossed over it. All the examples provided are far too complex for what I’m looking for.
-
Setting a whole Window’s Data Context now appears impossible. Do I need to use the
x:Bind
syntax for each control? -
As an extension of 2. above the examples I’ve seen are all setting the Data Context within the code-behind. Is this now the recommended approach?
I got this approach years ago mainly from reading Josh Smith’s website and some of the Reed Copsey’s excellent answers on this site like: What is the preferred way to connect viewmodels to their views?