title: Mvvm Light, UWP and code behind
body: <p>Base on <a href="https://www.youtube.com/watch?v=FrcVQGTXZKM" rel="nofollow">this excellent presentation</a> from Laurent Bugnion at Xamarin Evolve 2014, I'm trying to create my first UWP/MVVM Light application.</p>
  
  <p>I created a very simple <em>Article : ObservableObject</em> class with 2 string properties : <em>Référence</em> and <em>Désignation</em>.</p>
  
  <p>In the view model associated to the article list view, I have an action to create a new article :</p>
  
  <pre><code>    public ArticlesViewModel(IArticleService dataService, INavigationService navigationService)
      {
          ArticleService = dataService;
          NavigationService = navigationService;
  
          CréeArticleCommand = new RelayCommand(CréeArticle);
      }
  
      public RelayCommand CréeArticleCommand { get; private set; }
  
      private void CréeArticle()
      {
          if (!CréeArticleCommand.CanExecute(null))
              return;
  
          NavigationService.NavigateTo(ViewModelLocator.ArticleDetail_Key,
                                       new ArticleViewModel(new Article(),
                                                            ArticleService,
                                                            NavigationService));
      }
  </code></pre>
  
  <p>here is the XAML for my Article detail view :</p>
  
  <pre><code>&lt;!-- language: xaml --&gt;
  &lt;Page
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="using:UniversalTest1.UWP.Articles"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      xmlns:Editors="using:DevExpress.UI.Xaml.Editors"
      x:Class="UniversalTest1.UWP.Articles.Article_Detail"
      mc:Ignorable="d"
      xmlns:vm="clr-namespace:UniversalTest1.Data.ViewModels.Articles;assembly=UniversalTest1.Data"
      d:DataContext="{d:DesignInstance Type=vm:ArticleViewModel, IsDesignTimeCreatable=True}"&gt;
  
      &lt;Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"&gt;
          &lt;TextBlock Text="Référence :"   HorizontalAlignment="Left" Margin="24,15,0,0" VerticalAlignment="Top"/&gt;
          &lt;TextBlock Text="Désignation :" HorizontalAlignment="Left" Margin="10,52,0,0" VerticalAlignment="Top"/&gt;
  
          &lt;Editors:TextEdit Text="{Binding Article.Référence, Mode=TwoWay}" HorizontalAlignment="Left" Margin="100,8,0,0" VerticalAlignment="Top" Width="300"/&gt;
          &lt;Editors:TextEdit Text="{Binding Article.Désignation, Mode=TwoWay}" HorizontalAlignment="Left" Margin="100,45,0,0" VerticalAlignment="Top" Width="500"/&gt;
  
          &lt;Button Content="Sauver" Command="{Binding SauverCommand}" HorizontalAlignment="Left" Margin="102,84,0,0" VerticalAlignment="Top"/&gt;
      &lt;/Grid&gt;
  &lt;/Page&gt;
  </code></pre>
  
  <p>My problem here is that I have to define the DataContext in the code behind of my page :</p>
  
  <pre><code>public sealed partial class Article_Detail : Page
  {
      public Article_Detail()
      {
          this.InitializeComponent();
      }
  
      protected override void OnNavigatedTo(NavigationEventArgs e)
      {
          base.OnNavigatedTo(e);
          DataContext = (ArticleViewModel)e.Parameter;
      }
  }
  </code></pre>
  
  <p><strong>Is there a way to keep the design time DataContext as defined in the d:DataContext part of the Xaml's Page, and at runtime, get the DataContext from the Navigation parameter ?</strong></p>
  
  <p>My goal here is to have the less amount possible of code in the code behind. So I would like to define the runtime DataContext in the XAML also.</p>