112

The question pretty much says it all.

I have a window, and have tried to set the DataContext using the full namespace to the ViewModel, but I seem to be doing something wrong.

<Window x:Class="BuildAssistantUI.BuildAssistantWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    DataContext="BuildAssistantUI.ViewModels.MainViewModel">
4
  • Following up on Mike Nakis, I was trying to create the ViewModel manually and subscribe to events in it, only to find that the framework was creating another ViewModel. Hence the viewModel I was subscribed to was not the one attached to the view.
    – jlady
    Aug 27, 2020 at 21:21
  • Does this mean that besides instantiating the viewmodel yourself, you were also specifying the type of the viewmodel in some other way ? A secondary advantage of viewmodels requiring constructor parameters is that the framework either cannot instantiate them, or it has to pass default values for those parameters, in which case you can easily detect instantiation by the framework.
    – Mike Nakis
    Aug 28, 2020 at 8:13
  • The XAML designer might also need to be able to instantiate viewmodels, but this designer was never of any usefulness to me, (it just causes problems,) so I don't use it, so I personally do not care about that usage case.
    – Mike Nakis
    Aug 28, 2020 at 8:13
  • DataContext="{Binding Source={x:Type BuildAssistantUI.ViewModels.MainViewModel}}" can we use like this {x:Type}?.. but, it is not working. Dec 22, 2021 at 8:52

6 Answers 6

163

Try this instead.

<Window x:Class="BuildAssistantUI.BuildAssistantWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:VM="clr-namespace:BuildAssistantUI.ViewModels">
    <Window.DataContext>
        <VM:MainViewModel />
    </Window.DataContext>
</Window>
6
  • 3
    I like this option the best. Seems cleaner if the VM is only used for the MainWindow. Jan 29, 2013 at 0:36
  • 15
    Is there a way to set the data context using an attribute on the Window element, like DataContext="VM:MainWindowViewModel"?
    – Oliver
    Mar 18, 2014 at 15:21
  • This is the proper way!
    – JavierIEH
    Jul 26, 2015 at 22:13
  • I am not completely understanding totally why one way is better than the other. Also, I do not totally see the difference in either of these ways in comparison to how I have seen some people use "Dynamic Resource". What is this? Jun 5, 2017 at 14:32
  • 2
    @Oliver you would have to implement MarkupExtension, never done it on VMs, but you could do it with converters to ensure only one instance of converter is present and call it direcly from xaml with ="{converters:SomethingConverter}", implying xmlns:converters points at converter namespace. public abstract class BaseValueConverter<T> : MarkupExtension, IValueConverter where T : class, new() { private static T _converter; public override object ProvideValue(IServiceProvider serviceProvider) { return _converter ?? (_converter = new T()); } }
    – Whazz
    May 19, 2018 at 22:30
124

In addition to the solution that other people provided (which are good, and correct), there is a way to specify the ViewModel in XAML, yet still separate the specific ViewModel from the View. Separating them is useful for when you want to write isolated test cases.

In App.xaml:

<Application
    x:Class="BuildAssistantUI.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:BuildAssistantUI.ViewModels"
    StartupUri="MainWindow.xaml"
    >
    <Application.Resources>
        <local:MainViewModel x:Key="MainViewModel" />
    </Application.Resources>
</Application>

In MainWindow.xaml:

<Window x:Class="BuildAssistantUI.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    DataContext="{StaticResource MainViewModel}"
    />
6
  • Oh wow... thanks. I already marked this as answered, but your addition is much appreciated. Will use it.
    – Nicholas
    Jan 4, 2011 at 4:10
  • @Nicholas: The other answer is perfect for the question, so I agree with your decision Jan 4, 2011 at 4:15
  • 9
    Just be aware that this approach uses the same ViewModel instance for every instance of MainWindow. That's fine if the window is single-instance as this case implies, but not if you are showing multiple instances of the window such as in the case of a MDI or tabbed application.
    – Josh
    Jan 4, 2011 at 4:27
  • 1
    Actually Josh's answer is better as it gives you type-safety on the DataContext. So you can bind directly to the DataContext without worrying about typo-ing some property name/path.
    – Josh M.
    Nov 4, 2015 at 13:08
  • <TextBlock > <TextBlock.DataContext> <local:BaseViewModel/> </TextBlock.DataContext> </TextBlock> This works fine . but, i want like this inline <TextBlock DataContext="{Binding Souce={x:Type local:BaseViewModel}}" /> Dec 22, 2021 at 8:57
13

You need to instantiate the MainViewModel and set it as datacontext. In your statement it just consider it as string value.

     <Window x:Class="BuildAssistantUI.BuildAssistantWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:BuildAssistantUI.ViewModels">
      <Window.DataContext>
        <local:MainViewModel/>
      </Window.DataContext>
1
  • Thanks, I figured it was doing that.
    – Nicholas
    Jan 4, 2011 at 4:04
8

There is also this much better way of specifying the viewmodel:

    using Wpf = System.Windows;

    public partial class App : Wpf.Application //your skeleton app already has this.
    {
        protected override void OnStartup( Wpf.StartupEventArgs e ) //add this.
        {
            base.OnStartup( e );
            MainWindow = new MainView();
            MainWindow.DataContext = new MainViewModel( e.Args );
            MainWindow.Show();
        }
    }

The above mechanism applies to the main viewmodel only. To address a comment below which is asking what about user controls, the mechanism translates as follows for child viewmodels:

    public class ParentViewModel
    {
        public MyChildViewModel ChildViewModel { get; }
    
        public ParentViewModel()
        {
            ChildViewModel = new MyChildViewModel( ... );
        }
    }

ParentView.xaml:

    [...]
        xmlns:local="clr-namespace:the-namespace-of-my-wpf-stuff"
    [...]
        <local:MyChildView DataContext="{Binding ChildViewModel}" />
    [...]

<Rant>All of the solutions previously proposed require viewmodels to have parameterless constructors. Microsoft is under the impression that systems can be built using parameterless constructors. If you are also under that impression, the please, by all means, do go ahead and use some of the other solutions. For those who understand that constructors must have parameters, and therefore the instantiation of objects cannot be left in the hands of magic frameworks, the proper way of specifying the viewmodel is the one I showed above.</Rant>

2
  • What if I have a <UserControl> with a MainView and a MainViewModel - here I don't have an OnStartup event, this is actually what I'm looking into right now...
    – PandaWood
    Jul 31, 2021 at 13:59
  • @PandaWood I amended my answer to cover your question.
    – Mike Nakis
    Jan 4 at 10:10
3

You might want to try Catel. It allows you to define a DataWindow class (instead of Window), and that class automatically creates the view model for you. This way, you can use the declaration of the ViewModel as you did in your original post, and the view model will still be created and set as DataContext.

See this article for an example.

0

Setting it in the xaml did not work for me. I reviewed my code over and over and everthing looked fine. What solved my problem was setting the DataContext in the WindowIsLoaded event handler:

PrintDrsAnywayViewModel TheWindowsViewModel = new PrintDrsAnywayViewModel();

private void MyWindowIsLoaded(object sender, RoutedEventArgs e)
{
    DataContext = TheWindowsViewModel;
    TheWindowsViewModel.WindowHeaderMessage = "snot";
}

The xaml, which was the same before and after it worked is this:

<TextBlock Text="{Binding WindowHeaderMessage, Mode=OneWay}"
          Margin="20,20,0,20" FontSize="20" />

WindowHeaderMessage is a string property in the viewmodel file which implements INotifyPropertyChanged

hth

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.