title: DataGrid data binding and MVVM in WPF
body: <p>A WPF application with a <code>Window</code> containing a <code>DataGrid</code>, trying to implement the MVVM architecture. There is a single Model class:</p>
  
  <pre><code>public class Book
  {
      public int id {get; set;}
      public string title {get; set;}
      public string isbn {get; set;}
  }
  </code></pre>
  
  <p>This is the ViewModel:</p>
  
  <pre><code>class BookViewModel
  {
      public ObservableCollection&lt;Book&gt; Books;
  
      public BookViewModel()
      {
          Books = new ObservableCollection&lt;Book&gt;();
  
          // TODO: execute LoadData comand
      }
  }
  </code></pre>
  
  <p>A partial snippet of XAML code for markup for the View, which consists of a single Window:</p>
  
  <pre><code>&lt;Window.CommandBindings&gt;
      &lt;CommandBinding Command="self:CustomCommands.LoadData" CanExecute="LoadDataCommand_CanExecute" Executed="LoadDataCommand_Executed"/&gt;
  &lt;/Window.CommandBindings&gt;
  
  &lt;DataGrid Name="BooksDataGrid&gt;
      &lt;DataGrid.Columns&gt;
          &lt;DataGridTextColumn Header="Title" Width="200" Binding="{Binding title}"/&gt;
          &lt;DataGridTextColumn Header="isbn" Width="200" Binding="{Binding isbn}"/&gt;
      &lt;/DataGrid.Columns&gt;
  &lt;/DataGrid&gt;
  </code></pre>
  
  <p>The code behind for the View above:</p>
  
  <pre><code>public partial class MainWindow : Window
  {
      public MainWindow()
      {
          InitializeComponent();
  
          // TODO: execute LoadData command
      }
  
      private void LoadDataCommand_CanExecute(object sender, CanExecuteRoutedEventArgs args)
      {
          args.CanExecute = true;
      }
  
      private void LoadDataCommand_Executed(object sender, ExecutedRoutedEventArgs args)
      {
         // TODO: ask the model for the data
      }
  }
  
  public static class CustomCommands
  {
      public static readonly RoutedUICommand LoadData = new RoutedUICommand
          (
              "LoadData",
              "LoadData",
              typeof(CustomCommands),
              new InputGestureCollection()
              {
                  // allow Ctrl+L to perform this command
                  new KeyGesture(Key.L, ModifierKeys.Control)
              }
          );
  }
  </code></pre>
  
  <p>The codebehind for App.xaml:</p>
  
  <pre><code>public partial class App : Application
  {
      [STAThread()]
      public static void Main()
      {
          App app = new App();
          app.InitializeComponent();
          app.Run();
      }
  
      // bind application and show main window on startup
      // data context is default source of bindings
      protected override void OnStartup(StartupEventArgs e)
      {
          base.OnStartup(e);
  
          View.MainWindow mainWin = new View.MainWindow();
          ViewModel.BookViewModel bookViewModel = new ViewModel.BookViewModel();
          mainWin.DataContext = bookViewModel;
          mainWin.Show();
      }
  }
  </code></pre>
  
  <p>I am transitioning from WinForms to WPF, and find certain aspects of WPF confusing. What is trying to be achieved, and the problems faced, are as follows:</p>
  
  <ul>
  <li>the <code>LoadData</code> command should clean the <code>GridView</code>, then fill it again from a database. The database connection has already been implemented. So far, I can't see how to get the databinding working.</li>
  <li>the <code>LoadData</code> command should be executed from both a menu and button (already implemented).</li>
  <li>the <code>LoadData</code> command should be executed when the application starts. This is so the <code>DataGrid</code> would be filled when it starts.</li>
  </ul>