我正在開發一個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");
}
}
}
「最後一欄顯示的價格」,所以我是正確的,「價格」顯示,但是其他列不顯示? – StepUp
@StepUp,顯示所有列。我想說最後一欄包含我想要計算總和的價格。 – Cantinou
好的。顯示總價格。但是,您無法總計總價?你想要一個新的行在底部計算的總數量的價格? – StepUp