2015-11-06 87 views
0

我正在开发一个WPF-MVVM应用程序。DataGrid的DataGridColumn没有更新

我有一个空白的dataGrid,我添加了行。 最后一列显示价格。

而且我想显示的总价格作为衡量我添加行

我的代码不能正常工作。什么是问题?

查看

<DataGrid x:Name="dataGridInvoice" ItemsSource="{Binding Collection}" 
            AutoGenerateColumns="False" 
            SelectedItem="{Binding Selected, Mode=TwoWay}" 
        <DataGridComboBoxColumn Header="Ref Supplier" 
             ItemsSource="{Binding DataContext.Reference, Source={StaticResource ProxyElement}}" 
             DisplayMemberPath="refsup" 
             SelectedValueBinding="{Binding refSup, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
             SelectedValuePath="refsup"/> 
        <DataGridTextColumn Header="Quantity" Binding="{Binding quantity, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="*"/> 
        <DataGridTextColumn Header="Price/MOQ" Binding="{Binding unitPrice, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="*"/> 
        <DataGridTextColumn Header="Total Price" Binding="{Binding totalPrice, Mode=TwoWay, StringFormat=N2, UpdateSourceTrigger=PropertyChanged}" Width="*" IsReadOnly="True"/> 
       </DataGrid.Columns> 
      </DataGrid> 

视图模型

public class InvoiceViewModel : ViewModelBase 
    { 

     public Context ctx = new Context(); 

     Invoice invoice; 

     public InvoiceViewModel() 
     { 
      Collection = new ObservableCollection<PreInvoice>(); 
     } 

     private ObservableCollection<PreInvoice> collection; 

     public ObservableCollection<PreInvoice> Collection 
     { 
      get 
      { 
       return collection; 
      } 
      set 
      { 
       collection = value; 
       OnPropertyChanged("Collection"); 
       Total = Convert.ToString(Collection.Sum(t => t.totalPrice)); 

      } 
     } 

     private string _total; 
     public string Total 
     { 
      get 
      { 
       return _total; 
      } 
      set 
      { 
       _total = value; 
       OnPropertyChanged("Total"); 
      } 
     } 



     private void Save() 
     { 


     } 

     private void Delete() 
     { 

     } 




     #region "Command" 

     private ICommand saveCommand; 
     private ICommand removeCommand; 


     #endregion 

我的模型:

 # region wrapper 

     public class PreInvoice : ViewModelBase, IDataErrorInfo 
     { 

      private string _refSup; 

      public string refSup 
      { 
       get 
       { 
        return _refSup; 
       } 
       set 
       { 
        _refSup = value; 
        OnPropertyChanged("refSup"); 
       } 
      } 

      private decimal _quantity; 

      public decimal quantity 
      { 
       get 
       { 
        return _quantity; 
       } 
       set 
       { 
        _quantity = value; 
        OnPropertyChanged("quantity"); 
        totalPrice = _quantity * _unitPrice; 
       } 
      } 

      private decimal _unitPrice; 

      public decimal unitPrice 
      { 
       get 
       { 
        return _unitPrice; 
       } 
       set 
       { 
        _unitPrice = value; 
        OnPropertyChanged("unitPrice"); 
        totalPrice = _quantity * _unitPrice; 
       } 
      } 

      private decimal _totalPrice; 

      public decimal totalPrice 
      { 
       get 
       { 
        return _totalPrice; 
       } 
       set 
       { 
        _totalPrice = value; 
        OnPropertyChanged("totalPrice"); 

       } 
      } 
    } 
+0

“最后一栏显示的价格”,所以我是正确的,“价格”显示,但是其他列不显示? – StepUp

+0

@StepUp,显示所有列。我想说最后一栏包含我想要计算总和的价格。 – Cantinou

+0

好的。显示总价格。但是,您无法总计总价?你想要一个新的行在底部计算的总数量的价格? – StepUp

回答

1

更换为总资产的定义:您的收藏

private string _total; 
    public string Total 
    { 
     get 
     { 
      _total = Convert.ToString(Collection.Sum(t => t.totalPrice)); 
      return _total; 
     }    
    } 

手柄CollectionChanged事件:

public InvoiceViewModel() 
    { 
     Collection = new ObservableCollection<PreInvoice>(); 
     Collection.CollectionChanged += Collection_CollectionChanged; 
    } 

    void Collection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) 
    { 
     OnPropertyChanged("Total"); 
    } 
+0

谢谢!它工作正常! – Cantinou

+0

只是,我需要双击空白行来更新文本框 – Cantinou

+0

因此它创建一个空行 – Cantinou

0

看起来你混了的秩序声明在这个prorepty:

public ObservableCollection<PreInvoice> Collection 
    { 
     get 
     { 
      return collection; 
     } 
     set 
     { 
      collection = value; 
      Total = Convert.ToString(Collection.Sum(t => t.totalPrice)); 
      /*or if the above code is not working 
      collection=Collection.Sum(t => t.totalPrice)); 
      */ 
      OnPropertyChanged("Collection"); 
     } 
    } 

OnPropertyChanged( “全集”)方法通过发送数据( “文集”)PropertyChanged事件更新您的UI。

+0

我试过了,但它不起作用。 collection = Collection.Sum(t => t.totalPrice));无法将小数转换为'PreInvoice' – Cantinou