title: WPF DataGrid CanUserAddRows = True
body: <p>I seem to have a problem with adding rows to a <code>DataGrid</code>through the interface itself.
  Here is a screenshot of the UI:</p>
  
  <p><img src="https://lh6.googleusercontent.com/-FkdQQWfucWc/UtN89GBOXVI/AAAAAAAAAh0/M_aAe6OSYUI/s0/rbac-privilege-tab.png" alt="Screenshot" title="rbac-privilege-tab.png"></p>
  
  <p>As you can see, 0 rows were found in the database so nothing shows up in the <code>DataGrid</code> on the right side.
  But id like there to be one empty row there, for manually adding rows. 
  The <code>DataGrid.CanUserAddRows</code> is set to <code>True</code> but has no effect. 
  Here is the <code>xaml</code> for the <code>DataGrid</code>, I have taken the liberty of removing some of the code to make it smaller.</p>
  
  <p><strong>PrivilegeDetailsView.xaml</strong></p>
  
  <pre><code>&lt;UserControl ...
               d:DataContext="{d:DesignInstance impl:PrivilegeDetailsViewModel}"&gt;
      &lt;DataGrid ...
                ItemsSource="{Binding RolesHasPrivilegesOnObjects}" 
                AutoGenerateColumns="False"
                CanUserAddRows="True"&gt;
          &lt;DataGrid.Columns&gt;
              &lt;DataGridTemplateColumn Header="Type" CanUserSort="True"&gt;
                  &lt;DataGridTemplateColumn.CellTemplate&gt;
                      &lt;DataTemplate DataType="{x:Type int:IRoleHasPrivilegeOnObjectListItemViewModel}"&gt;
                          &lt;Image Source="{Binding Icon}" ToolTip="{Binding ToolTip}"/&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/DataGridTemplateColumn.CellTemplate&gt;
              &lt;/DataGridTemplateColumn&gt;
              &lt;DataGridTextColumn Width="*" Header="Name" Binding="{Binding Name}"/&gt;
              &lt;DataGridCheckBoxColumn Header="Select" Binding="{Binding HasSelect, UpdateSourceTrigger=PropertyChanged}"&gt;
                  &lt;DataGridCheckBoxColumn.ElementStyle&gt;
                      &lt;Style TargetType="CheckBox"&gt;
                          &lt;Style.Triggers&gt;
                              &lt;DataTrigger Binding="{Binding CanHaveSelect}" Value="True"&gt;
                                  &lt;Setter Property="IsEnabled" Value="True"/&gt;
                                  &lt;Setter Property="HorizontalAlignment" Value="Center"/&gt;
                              &lt;/DataTrigger&gt;
                              &lt;DataTrigger Binding="{Binding CanHaveSelect}" Value="False"&gt;
                                  &lt;Setter Property="IsEnabled" Value="False"/&gt;
                                  &lt;Setter Property="HorizontalAlignment" Value="Center"/&gt;
                              &lt;/DataTrigger&gt;
                          &lt;/Style.Triggers&gt;
                      &lt;/Style&gt;
                  &lt;/DataGridCheckBoxColumn.ElementStyle&gt;
              &lt;/DataGridCheckBoxColumn&gt;
              ...
          &lt;/DataGrid.Columns&gt;
      &lt;/DataGrid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p><strong>PrivilegeDetailsView.xaml.cs</strong></p>
  
  <pre><code>public partial class PrivilegeDetailsView : IPrivilegeDetailsView
  {
      public PrivilegeDetailsView() { InitializeComponent(); }
  
      public DataGrid PrivilegesOnObjectsDataGrid { get { return PrivilegeDataGrid; } }
  
      public IViewModel ViewModel
      {
          get { return (IViewModel)DataContext; }
          set { DataContext = value; }
      }
  }
  </code></pre>
  
  <p>Here is the <code>ViewModel (VM)</code> for the <code>xaml</code> <code>View</code> above:</p>
  
  <p><strong>PrivilegeDetailsViewModel.cs</strong></p>
  
  <pre><code>public class PrivilegeDetailsViewModel : ViewModelBase, IPrivilegeDetailsViewModel
  {
      private readonly IEventAggregator _eventAggregator;
      private readonly IPrivilegeViewModel _privilegeViewModel;
      private readonly IRoleHasPrivilegeOnObjectViewModelAdapterRepository _roleHasPrivilegeOnObjectViewModelAdapterRepository;
      private ObservableCollection&lt;IRoleHasPrivilegeOnObjectListItemViewModel&gt; _rolesHasPrivilegesOnObjects;
  
      public PrivilegeDetailsViewModel(IPrivilegeDetailsView view,
                                       IRoleHasPrivilegeOnObjectViewModelAdapterRepository roleHasPrivilegeOnObjectViewModelAdapterRepository,
                                       IPrivilegeViewModel privilegeViewModel,
                                       IEventAggregator eventAggregator) : base(view)
      {
          _roleHasPrivilegeOnObjectViewModelAdapterRepository = roleHasPrivilegeOnObjectViewModelAdapterRepository;
          _privilegeViewModel = privilegeViewModel;
          _eventAggregator = eventAggregator;
          Initialize();
      }
  
      protected override sealed void Initialize()
      {
          _privilegeViewModel.PropertyChanged += PrivilegeViewModelOnPropertyChanged;
          _eventAggregator.GetEvent&lt;ToggleSelectPrivilegeEvent&gt;().Subscribe(ToggleSelectPrivilege);
          ...
      }
  
      public new IPrivilegeDetailsView View
      {
          get { return (IPrivilegeDetailsView)base.View; }
      }
  
      public ObservableCollection&lt;IRoleHasPrivilegeOnObjectListItemViewModel&gt; RolesHasPrivilegesOnObjects
      {
          get { return _rolesHasPrivilegesOnObjects; }
          set
          {
              _rolesHasPrivilegesOnObjects = value;
              OnPropertyChanged();
          }
      }
  
      public void Save()
      {
          if(RolesHasPrivilegesOnObjects == null) return;
          _roleHasPrivilegeOnObjectViewModelAdapterRepository.SaveChanges(RolesHasPrivilegesOnObjects);
      }        
  
      private void ToggleExecutePrivilege(object obj)
      {
          var toggle = !View.PrivilegesOnObjectsDataGrid.SelectedItems.Cast&lt;IRoleHasPrivilegeOnObjectListItemViewModel&gt;()
                            .All(x =&gt; x.HasExecute);
          foreach(var selectedItem in View.PrivilegesOnObjectsDataGrid
                                          .SelectedItems
                                          .Cast&lt;IRoleHasPrivilegeOnObjectListItemViewModel&gt;()
                                          .Where(selectedItem =&gt; selectedItem.Object
                                                                             .CanHavePrivilege("EXECUTE"))) {
                                              selectedItem.HasExecute = toggle; 
                                          }
      }
  
      ...
  
      private void PrivilegeViewModelOnPropertyChanged(object s, PropertyChangedEventArgs e)
      {
          switch(e.PropertyName)
          {
              //When the SelectedSchema changes in the parent VM, I get the new rows to be shown in the DataGrid.
              case "SelectedSchema":
                  RolesHasPrivilegesOnObjects = _roleHasPrivilegeOnObjectViewModelAdapterRepository
                                                    .GetPrivilegesOnObjectsAssociatedWith((IRoleEntityViewModel)_privilegeViewModel.SelectedRole,
                                                    (IContainerEntityViewModel)_privilegeViewModel.SelectedSchema);
                  break;
          }
      }
  }
  </code></pre>
  
  <p>This is the <code>VM</code> for each row in the <code>DataGrid</code></p>
  
  <p><strong>RoleHasPrivilegeOnObjectEntityViewModel.cs</strong></p>
  
  <pre><code>public class RoleHasPrivilegeOnObjectEntityViewModel : EntityViewModelBase&lt;RoleHasPrivilegeOnObjectEntityViewModel, 
                                                         RoleHasPrivilegesOnObject&gt;, 
                                                         IRoleHasPrivilegeOnObjectListItemViewModel
  {
      private readonly RoleHasPrivilegesOnObject _roleHasPrivilegesOnObject;
  
      public RoleHasPrivilegeOnObjectEntityViewModel(RoleHasPrivilegesOnObject roleHasPrivilegesOnObject)
      {
          _roleHasPrivilegesOnObject = roleHasPrivilegesOnObject;
          Role = new RoleEntityViewModel(_roleHasPrivilegesOnObject.Role);
          Object = new ObjectEntityViewModel(_roleHasPrivilegesOnObject.Object);
      }
  
      public override EntityType EntityType { get { return EntityType.NONE; } }
  
      public override RoleHasPrivilegesOnObject OriginalEntity { get { return _roleHasPrivilegesOnObject; } }
  
      public IRoleEntityViewModel Role { get; set; }
  
      public IObjectEntityViewModel Object { get; set; }
  
      public string ToolTip { get { return _roleHasPrivilegesOnObject.ToolTip; } }
  
      public bool HasExecute
      {
          get { return _roleHasPrivilegesOnObject.HasExecute; }
          set
          {
              _roleHasPrivilegesOnObject.HasExecute = value;
              OnPropertyChanged();
          }
      }
  
      public bool CanHaveExecute { get { return _roleHasPrivilegesOnObject.CanHaveExecute; } }
  
      public override string Icon { get { return Object != null ? Object.Icon : string.Empty; } }
  
      public override string NAME
      {
          get { return _roleHasPrivilegesOnObject.NAME; }
          set
          {
              _roleHasPrivilegesOnObject.NAME = value;
              OnPropertyChanged();
          }
      }
  
      ...
  }
  </code></pre>
  
  <p>I know this is a lot of code, I have stripped away a lot and put in place a few dots (...) to show that more code exist. NOTE: Im using <a href="http://msdn.microsoft.com/en-us/data/ef.aspx">EF5</a> and <a href="http://compositewpf.codeplex.com/">PRISM</a></p>
  
  <p><strong>How can I make the <code>DataGrid</code> accept new rows through the GUI?</strong></p>