Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the advantage of setting DataContext in code instead of XAML?
    primarykey
    data
    text
    <p>There seem to be two main ways to define DataContext in WPF:</p> <ul> <li>either <strong>in code</strong> like this:</li> </ul> <p>App.xaml.cs (taken from the <a href="http://www.codeplex.com/wpf/Release/ProjectReleases.aspx?ReleaseId=14962" rel="noreferrer">WPF MVVM Toolkit template</a>):</p> <pre><code>public partial class App : Application { private void OnStartup(object sender, StartupEventArgs e) { // Create the ViewModel and expose it using the View's DataContext MainView mainView = new MainView(); MainViewModel mainViewModel = new MainViewModel(); mainViewModel.LoadCustomers("c:\\testdata2\\Customers.xml"); mainView.DataContext = mainViewModel; mainView.Show(); } } </code></pre> <ul> <li>or <strong>in XAML</strong> like this:</li> </ul> <p>Window1.xaml:</p> <pre><code>&lt;DockPanel&gt; &lt;StackPanel HorizontalAlignment="Left" DockPanel.Dock="Top" Orientation="Horizontal"&gt; &lt;StackPanel.DataContext&gt; &lt;local:CustomerViewModel /&gt; &lt;/StackPanel.DataContext&gt; &lt;TextBlock Text="{Binding Path=FirstName}" /&gt; &lt;TextBlock Text=" " /&gt; &lt;TextBlock Text="{Binding Path=LastName}" /&gt; &lt;/StackPanel&gt; &lt;StackPanel HorizontalAlignment="Left" VerticalAlignment="top" DockPanel.Dock="Top" Orientation="Horizontal"&gt; &lt;ListBox ItemsSource="{Binding Source={StaticResource FileNames}}" /&gt; &lt;/StackPanel&gt; &lt;StackPanel HorizontalAlignment="Left" VerticalAlignment="top" DockPanel.Dock="Top" Orientation="Horizontal"&gt; &lt;ComboBox ItemsSource="{Binding Source={StaticResource Directories}}" SelectedIndex="0" /&gt; &lt;/StackPanel&gt; &lt;StackPanel HorizontalAlignment="Left" VerticalAlignment="top" DockPanel.Dock="Top" Orientation="Horizontal"&gt; &lt;StackPanel.DataContext&gt; &lt;local:SystemInformationViewModel /&gt; &lt;/StackPanel.DataContext&gt; &lt;TextBlock Text="{Binding Path=CurrentTime}" /&gt; &lt;/StackPanel&gt; &lt;/DockPanel&gt; </code></pre> <p>One advantage that defining the DataContext in XAML has is that <strong>your data shows up in Expression Blend design mode</strong> and Expression Blend allows you to do quite a lot within the GUI e.g. choose fields from your datasource, etc. <a href="http://tanguay.info/web/index.php?pg=codeExamples&amp;id=176" rel="noreferrer">as shown here</a>.</p> <p>I have read that binding <strong>ADO.NET objects cannot be bound in XAML</strong> (although I don't see why you could write a minimal wrapper for them to which you could bind from XAML).</p> <p>Strange that the <strong>WPF Team in making the WPF MVVM templates define the DataContext in code</strong> which very quickly makes it impracticable to edit your Views in Expression Blend, since your data doesn't show up in design mode which is often a significant part of the layout.</p> <p>So I'm thinking there <strong>must be some advantage</strong> down the road to setting the DataContext in code instead of XAML, anyone know what it is?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload