Adding rows into WPF DataGrid

Jul 23, 2009 at 1:56 PM

I can't see the empty row at the bottom of my DataGrid, which is used to add items. What am I doing wrong? Which conditions should be met so that DataGrid would allow me to add items?

Here is the datagrid-related code (the real number of columns is bigger...):

1. XAML:

 

      <!-- Transactions data grid -->
      <tk:DataGrid x:Name="grdTransactions" ItemsSource="{Binding Transactions}" IsSynchronizedWithCurrentItem="True"
                   AutoGenerateColumns="False" SelectionMode="Single"
                   RowHeight="25" 
                   CanUserResizeRows="False" CanUserAddRows="True">
         <tk:DataGrid.Columns>
            
            <tk:DataGridTemplateColumn Header="Deal Date" MinWidth="120"
                                       HeaderStyle="{StaticResource CenterAlignedColumnHeaderStyle}"
                                       CellStyle="{StaticResource CenterAlignedCellStyle}"  >
               <tk:DataGridTemplateColumn.CellTemplate>
                  <DataTemplate>
                     <TextBlock Text="{Binding DealDate, StringFormat=dd-MMM-yy}" />
                  </DataTemplate>
               </tk:DataGridTemplateColumn.CellTemplate>
               <tk:DataGridTemplateColumn.CellEditingTemplate>
                  <DataTemplate>
                     <tk:DatePicker SelectedDate="{Binding DealDate}" Cursor="Hand" />
                  </DataTemplate>
               </tk:DataGridTemplateColumn.CellEditingTemplate>
            </tk:DataGridTemplateColumn>
            
            <tk:DataGridTextColumn Header="Shareholder" Binding="{Binding Shareholder}" 
                                   CellStyle="{StaticResource LeftAlignedCellStyle}"/>
            
            <tk:DataGridTextColumn Header="Bank" Binding="{Binding Bank}" IsReadOnly="True" 
                                   CellStyle="{StaticResource LeftAlignedCellStyle}" />
            
            <tk:DataGridTemplateColumn Header="Status" MinWidth="120"
                                       HeaderStyle="{StaticResource CenterAlignedColumnHeaderStyle}"
                                       CellStyle="{StaticResource CenterAlignedCellStyle}" >
               
               <tk:DataGridTemplateColumn.CellTemplate>
                  <DataTemplate>
                     <TextBlock Text="{Binding Status.Caption}" />
                  </DataTemplate>
               </tk:DataGridTemplateColumn.CellTemplate>
               <tk:DataGridTemplateColumn.CellEditingTemplate>
                  <DataTemplate>
                     <ComboBox SelectedItem="{Binding Status}" DisplayMemberPath="Caption" Cursor="Hand"
                               ItemsSource="{Binding DataContext.StatusValues, 
                                                     RelativeSource={RelativeSource AncestorType={x:Type Window}}, 
                                                     IsAsync=true}" />
                  </DataTemplate>
               </tk:DataGridTemplateColumn.CellEditingTemplate>
            </tk:DataGridTemplateColumn>
           
            <tk:DataGridTextColumn Header="Received" Binding="{Binding Received, StringFormat=N2}" 
                                   HeaderStyle="{StaticResource CenterAlignedColumnHeaderStyle}"
                                   CellStyle="{StaticResource RightAlignedCellStyle}" />
            
            <tk:DataGridTextColumn Header="Price" Binding="{Binding Price, StringFormat=N2}" IsReadOnly="True"
                                   HeaderStyle="{StaticResource CenterAlignedColumnHeaderStyle}"
                                   CellStyle="{StaticResource RightAlignedCellStyle}" />
            
            <tk:DataGridTextColumn Header="Number" Binding="{Binding Number, StringFormat=N4}" IsReadOnly="True"
                                   HeaderStyle="{StaticResource CenterAlignedColumnHeaderStyle}"
                                   CellStyle="{StaticResource RightAlignedCellStyle}" />
         </tk:DataGrid.Columns>
      </tk:DataGrid>

2. ViewModel:

    public abstract class ViewModelBase : INotifyPropertyChanged, IEditableCollectionView
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #region IEditableCollectionView Members

        public object AddNew()
        {
            throw new NotImplementedException();
        }

        public bool CanAddNew
        {
            get { return true; }
        }

        public bool CanCancelEdit
        {
            get { throw new NotImplementedException(); }
        }

        public bool CanRemove
        {
            get { throw new NotImplementedException(); }
        }

        public void CancelEdit()
        {
            throw new NotImplementedException();
        }

        public void CancelNew()
        {
            throw new NotImplementedException();
        }

        public void CommitEdit()
        {
            throw new NotImplementedException();
        }

        public void CommitNew()
        {
            throw new NotImplementedException();
        }

        public object CurrentAddItem
        {
            get { throw new NotImplementedException(); }
        }

        public object CurrentEditItem
        {
            get { throw new NotImplementedException(); }
        }

        public void EditItem(object item)
        {
            throw new NotImplementedException();
        }

        public bool IsAddingNew
        {
            get { throw new NotImplementedException(); }
        }

        public bool IsEditingItem
        {
            get { throw new NotImplementedException(); }
        }

        public NewItemPlaceholderPosition NewItemPlaceholderPosition
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public void Remove(object item)
        {
            throw new NotImplementedException();
        }

        public void RemoveAt(int index)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

 

 

    public class MainViewModel : ViewModelBase
    {
        public MainViewModel()
        {
            db = FundkeeperModel.GetDataContext();
            transactions = new ObservableCollection<TransactionViewModel>(
                from t in db.Transactions
                select new TransactionViewModel(t));
            transactionsView = CollectionViewSource.GetDefaultView(transactions);
        }

        #region Properties

        public ObservableCollection<TransactionViewModel> Transactions
        {
            get { return transactions; }
        }

        public IEnumerable<Status> StatusValues
        {
            get { return db.Status.AsEnumerable(); }
        }

        #endregion

        #region Private Members

        private FundkeeperDataContext db;
        private ObservableCollection<TransactionViewModel> transactions;
        private ICollectionView transactionsView;

        #endregion
    }

 

 

    public class TransactionViewModel : ViewModelBase
    {
        public TransactionViewModel(Transaction transaction)
        {
            this.transaction = transaction;
        }

        #region Binding Properties

        public DateTime DealDate
        {
            get { return transaction.DealDate; }
            set { transaction.DealDate = value; }
        }

        public string Shareholder
        {
            get { return transaction.Shareholder.Name; }
            set
            {
            }
        }

        public string Bank
        {
            get { return transaction.Owner.Bank.Name; }
            set
            {
            }
        }

        public Status Status
        {
            get { return transaction.Status; }
            set { transaction.Status = value; }
        }

        public decimal Received
        {
            get { return transaction.Received ?? 0; }
            set {
                transaction.Received = value;
                this.RecalculateFields();
            }
        }

        public decimal Price
        {
            get { return transaction.SharePrice.Price; }
        }

        public decimal Number
        {
            get { return this.Final / this.Price; }
        }

        public bool IsSelected { get; set; }

        #endregion

        #region Private Members

        void RecalculateFields()
        {
            OnPropertyChanged("Dealing");
            OnPropertyChanged("Final");
            OnPropertyChanged("Number");
        }

        Transaction transaction;

        #endregion
    }

Thank you!