29

I want to be able to create an instance of the DataContext object for my WPF StartupUri window in XAML, as opposed to creating it code and then setting the DataContext property programmaticly.

The main reason is I don't need to access the object created externally and I don't want to have to write code behind just for setting the DataContext.

I'm sure I've read somewhere how to instantiate the DataContext object in XAML but I can't find it in any of the usual places...

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Gus Paul
  • 945
  • 2
  • 9
  • 17

4 Answers4

34

You add an XML namespace for whatever namespace your DataContext lives in, create an instance of it in the Window Resources and set the DataContext to that resource:

<Window x:Class="WpfApplication4.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication4"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <local:MyViewModel x:Key="MyViewModel"/>
    </Window.Resources>
    <Grid DataContext="{StaticResource MyViewModel}">

    </Grid>
</Window>
Steven Robbins
  • 26,441
  • 7
  • 76
  • 90
  • Does this work in .NET framework 4.5? I tried this code and it says ... `The type 'local:MyViewModel' was not found. Verify that you are not missing an assembly and that all referenced assemblies have been built.` – Rafaf Tahsin Mar 08 '16 at 09:21
  • @RafafTahsin did you ensure you had the namespace correctly? Also, if you added the referenced type to the project but have not yet built, the WPF designer gets a little confused. Have you tried building? – Randolpho Aug 09 '16 at 17:22
  • If You are also using a `ResourceDictionary`, `local:MyViewModel` goes inside the dictionary (but any `MergedDictionary`) – xvan May 06 '23 at 05:35
28

You can just specify this directly in XAML for the entire Window:

<Window 
    ... xmlns definitions ...
>
   <Window.DataContext>
        <local:CustomViewModel />
   </Window.DataContext>
</Window>

This creates a view model named "CustomViewModel" in the namespace aliased to local, directly as the DataContext for the Window.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
16

Assuming this code:

public abstract class BaseView { }
public class RuntimeView : BaseView { }
public class DesigntimeView : BaseView { }

Try this:

<Page.DataContext>
    <local:RuntimeView />
</Page.DataContext>
<d:Page.DataContext>
    <local:DesigntimeView />
</d:Page.DataContext>
<ListBox ItemsSource="{Binding}" />

Best of luck!

Jerry Nixon
  • 31,313
  • 14
  • 117
  • 233
  • 1
    +1 because this shows both run and designtime, and doesn't make use of x:Key but instead places the DataContext directly in the element where it belongs – stijn Dec 04 '12 at 12:03
0

If you need to set the DataContext as same control class:

    <Window x:Class="TabControl.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        
            xmlns:local="clr-namespace:TabControl"
            Title="MainWindow" Height="350" Width="525"
            DataContext="{Binding RelativeSource={RelativeSource Self}}"        
            >
</Window>

use RelativeSource binding.

or just

     <Window x:Class="TabControl.MainWindow"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        
                xmlns:local="clr-namespace:TabControl"
                Title="MainWindow" Height="350" Width="525"                        
                >
<Window.DataContext>
< new instance of any viewModel here....>
</Window.DataContext>
    </Window>

If want to assign an instance of different class than itself.

Kylo Ren
  • 8,551
  • 6
  • 41
  • 66