Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to instantiate DataContext object in XAML

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...

like image 625
Gus Paul Avatar asked Oct 05 '09 18:10

Gus Paul


People also ask

What is DataContext in XAML?

The XAML in WPF is just a pretty user interface to display and interact with the actual data, otherwise known as the DataContext . The purpose of other binding sources ( RelativeSource , ElementName , etc) is to point to another property that doesn't exist in the current control's DataContext.

How do you define DataContext?

DataContext> syntax. You assign the class that represents the data context object to the view, not an individual property so {Binding Employee} is invalid here, you just have to specify an object. know that you are creating a new instance of the Employee class and assigning it as the data context object.

What is binding in XAML?

Advertisements. Data binding is a mechanism in XAML applications that provides a simple and easy way for Windows Runtime Apps using partial classes to display and interact with data. The management of data is entirely separated from the way the data is displayed in this mechanism.

What is binding source in WPF?

A binding source is usually a property on an object so you need to provide both the data source object and the data source property in your binding XAML. In the above example the ElementName attribute signifies that you want data from another element on the page and the Path signifies the appropriate property.


2 Answers

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> 
like image 133
Steven Robbins Avatar answered Sep 24 '22 03:09

Steven Robbins


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.

like image 32
Reed Copsey Avatar answered Sep 25 '22 03:09

Reed Copsey